let masterList=[{id:1,name:'Abc'},{id:2,name:'Def'},{id:3,name:'Ghi'}];
let selectedList=[2,3];
The desired result is to have
//desiredList=[{id:2,name:'Def'},{id:3,name:'Ghi'}]
Currently what I am doing is
let parsedArray = [];
masterList.forEach(mItem => {
selectedList.forEach(sItem => {
if (mItem.id === sItem) {
parsedArray.push(mItem);
}
});
});
desiredList=parsedArray
I do not find this method efficient when iterating over large arrays, is there any logic, any inbuilt javascript operators using which I can achieve the same?
let masterList=[{id:1,name:'Abc'},{id:2,name:'Def'},{id:3,name:'Ghi'}];
let selectedList=[2,3];
The desired result is to have
//desiredList=[{id:2,name:'Def'},{id:3,name:'Ghi'}]
Currently what I am doing is
let parsedArray = [];
masterList.forEach(mItem => {
selectedList.forEach(sItem => {
if (mItem.id === sItem) {
parsedArray.push(mItem);
}
});
});
desiredList=parsedArray
I do not find this method efficient when iterating over large arrays, is there any logic, any inbuilt javascript operators using which I can achieve the same?
Share Improve this question asked Jul 8, 2019 at 10:16 PCKPCK 1,4446 gold badges23 silver badges38 bronze badges 7- how large is a large array in this case? – Davin Tryon Commented Jul 8, 2019 at 10:23
- Do you want the code to shorter/simpler or you want to have a single loop instead of nested loops? – Maheer Ali Commented Jul 8, 2019 at 10:25
- does the order matters? – Nina Scholz Commented Jul 8, 2019 at 10:26
- @MaheerAli I'd say having a simpler code and having a simple loop is a win win situation. But, I'd primarily would want the code to be faster and effecient. – PCK Commented Jul 8, 2019 at 10:28
- 2 50 is not enough elements to worry about optimizing for performance. Unless you have numbers that specifically show otherwise, I'd focus on making the lookup readable and measure. – Davin Tryon Commented Jul 8, 2019 at 10:42
6 Answers
Reset to default 6You could take a map
with id
as key and the object as value and map the wanted values from the map by mapping selectedList
.
This approach uses the order from selectedList
.
var masterList = [{ id: 1, name: 'Abc' }, { id: 2, name: 'Def' }, { id: 3, name: 'Ghi' }],
selectedList = [2, 3],
result = selectedList.map(Map.prototype.get, new Map(masterList.map(o => [o.id, o])));
console.log(result);
It should be a simple filter
on the masterList
:
masterList.filter(item => selectedList.includes(item.id));
You can use Array filter()
to do this.
Demo:
let masterList=[{id:1,name:'Abc'},{id:2,name:'Def'},{id:3,name:'Ghi'}];
let selectedList=[2,3];
let desiredList = masterList.filter((val) => selectedList.includes(val.id));
console.log(desiredList)
You can first convert selectedList
to Set
and then use filter()
method array of objects.
You can use Set.prototype.has
to check whether the id
of the objects exists in the set or not. And this method has O(1) time-complexity. So the time-complexity of the whole algorithm will be linear.
let masterList=[{id:1,name:'Abc'},{id:2,name:'Def'},{id:3,name:'Ghi'}];
let selectedList = [2,3];
let set = new Set(selectedList);
let res = masterList.filter(x => set.has(x.id));
console.log(res)
Turn the first array into an object indexed by id
first, so you can look up the appropriate matching object in O(1)
time, and then you can .map
the selectedList
:
const masterList=[{id:1,name:'Abc'},{id:2,name:'Def'},{id:3,name:'Ghi'}];
const selectedList=[2,3,4];
const masterById = masterList.reduce((a, obj) => {
a[obj.id] = obj;
return a;
}, {});
const desiredList = selectedList.map(id => masterById[id]).filter(Boolean);
console.log(desiredList);
Try this:
let masterList=[{id:1,name:'Abc'},{id:2,name:'Def'},{id:3,name:'Ghi'}];
let selectedList=[2,3];
const result = masterList.filter(({id})=> selectedList.includes(id));
console.log(result);