JSFiddle - React, Tailwind, and code Playground
by jboesch26
HTML
<div>something</div>
<div>another</div>
<div>something else</div>
<div>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the lea</div>
JavaScript
//
// New map with support for objects. It would be cool.. except it's definitely not as
// performant. The current $.map implementation just uses a for loop. It's pretty
// impossible to enhance the function to be able to handle objects without it getting
// a performance hit. I guess it's why this ticket never went through...
// http://bugs.jquery.com/ticket/2616
// $.new_map is ~50% slower with it being able to handle objects
// JS Perf test: http://jsperf.com/old-map-vs-new-map-with-obj-support
$.new_map = function(elems, callback, arg) {
var ret = [],
value;
$.each(elems, function(key, value) {
value = callback(value, key, arg);
if (value != null) {
ret[ret.length] = value;
}
});
return ret.concat.apply([], ret);
}
$.each(['map', 'new_map'], function(i, func){
console.log('---------' + func + '------------');
var ks = $[func]([1, 2, 4, 6], function(v, k) {
return k;
});
console.log(ks);
var keys = $[func]({
a: 1,
b: 2
}, function(v, k) {
return k;
});
console.log(keys);
var values = $[func]({
a: 1,
b: 2
}, function(v, k) {
return v;
});
console.log(values);
var mapped = $[func](document.getElementsByTagName('div'), function(v, k) {
return v;
});
console.log(mapped);
var flat = $[func](Array(8), function(v, k) {
return k % 2 ? k : [k, k, k]; //try mixing array and regular returns
});
console.log(flat);
});