javascript - Get specific items from array -
what need specific items (defined array if indices) array. let have source array [2,4,1,6,8] , array of indices [0,3] , want result [2,6]. far doing achieve result
var iter = -1; source.filter(function(item) {iter++; if (indices.indexof(iter)>-1) {return item}})
is there more elegant solution (maybe javascript syntactic sugar not aware of) this?
you can use map:
var source = [2, 4, 1, 6, 8]; var indices = [0, 3]; var output = indices.map(function(i){ return source[i] });
Comments
Post a Comment