javascript - Extracting IDs from an array -
i have array containing arrays different lengths. these arrays contain objects ids properties 1 below:
[ [{ docid: 'ep/l007177/1', weight: 0.60196078431373 }, { //... }, //... ], [{ //... }], // ... ] then have array of ids each time.
example of other array:
docs:[ { id: 'as/p003/5' }, { id: 'ep/l007177/1'} ,......] how can extract (unique) numbers of indexes in original array contain ids have in other array?
i made function, work if give 1 id it:
var gettopics = function (ids, data) { var indexes = []; data.foreach(function(d, i) { d.foreach(function(f) { if ((ids.indexof(f.docid) > -1) && (indexes.indexof(i) === -1)) { indexes.push(i); } }); }); return indexes; } if do:
docs.foreach(function(o) { list = gettopics(o.id,dataa) }) i 1 index, not expect.
as understand want find object id inside nested arrays (than may want it).
here sample how can it:
var arr1 = [ [{id: 1, data: 'xxx'}, {id: 2, data: 'yyy'}], [{id: 3, data: 'zzz'}]]; function finbyid(id) { var result = null; arr1.foreach((row) => { row.foreach((doc) => { if(doc.id === id) result = doc; }) }) return result; }
Comments
Post a Comment