how to create dynamic key and valuse in javascript array -
input json
0===>{"eid":12,"gender":"1","age":1,"pass":["2","1"]} 1===>{"eid": 11,"gender":"0","age":1,"pass":["1","3"]} 2===>{"eid":20,"gender":"1","age":1,"pass":["2","3"]}
how create new array.. push ids based on pass numbers
ex: in loop display
passid => 2 .... eid => 12, 20
2 ==> ["12","20"] 1 ==> [12, 11] 3 ==> [11,20]
use filter
, some
check contents of pass
array , return respective eid
values:
function grabber(data, pass) { return data.filter(function (el) { return el.pass.some(function (num) { return +num === pass; }) }).map(function (el) { return el.eid; }); } grabber(data, 1); // [12, 11] grabber(data, 2); // [12, 20] grabber(data, 3); // [11, 20]
update
realised on way home work don't need some
. further, answer comment, here's how might search pass
, gender
:
function grabber(data, options) { return data.filter(function (el) { return el.pass.indexof(options.pass) > -1 && el.gender === options.gender; }).map(function (el) { return el.eid; }); } grabber(data, { gender: '0', pass: '1' }); // [11]
Comments
Post a Comment