i need to update the array of objects based on another array in ES6. Example
let a = [
{ id : 23, active :false, status:"" },
{ id : 33, active :false, status:"" },
{ id : 167, active :false, status:"" },
{ id : 18, active :false, status:"" },
{ id : 200, active :false, status:"" },
]
my second array which contain the objects
let marked = [167,33,23];
expected results as below
let a = [
{ id : 23, active :true, status:"updated"},
{ id : 33, active :true, status:"updated" },
{ id : 167, active :true, status:"updated" },
{ id : 18, active :false, status:"" },
{ id : 200, active :false, status:"" },
]
please let me know how to get as expected result. do we have any other methods in lodash for this.?
i need to update the array of objects based on another array in ES6. Example
let a = [
{ id : 23, active :false, status:"" },
{ id : 33, active :false, status:"" },
{ id : 167, active :false, status:"" },
{ id : 18, active :false, status:"" },
{ id : 200, active :false, status:"" },
]
my second array which contain the objects
let marked = [167,33,23];
expected results as below
let a = [
{ id : 23, active :true, status:"updated"},
{ id : 33, active :true, status:"updated" },
{ id : 167, active :true, status:"updated" },
{ id : 18, active :false, status:"" },
{ id : 200, active :false, status:"" },
]
please let me know how to get as expected result. do we have any other methods in lodash for this.?
Share Improve this question edited Oct 24, 2021 at 3:26 Sanjai kumar asked Oct 23, 2021 at 19:15 Sanjai kumarSanjai kumar 552 silver badges9 bronze badges 5- I assume you want {id:33, active:true} as well? – Burakhan Aksoy Commented Oct 23, 2021 at 19:19
- yes @BurakhanAksoy for 33, 23 and 167 – Sanjai kumar Commented Oct 23, 2021 at 19:20
- can you explain more please – Darshan Raval Commented Oct 23, 2021 at 19:33
- there is typo that 18 has true isn't it? – gmoniava Commented Oct 23, 2021 at 19:34
- yes @GiorgiMoniava 18 isn't true – Sanjai kumar Commented Oct 24, 2021 at 3:27
3 Answers
Reset to default 4You don't need lodash, you could do this:
let a = [{
id: 23,
active: false
},
{
id: 33,
active: false
},
{
id: 167,
active: false
},
{
id: 18,
active: false
},
{
id: 2,
active: false
},
]
let marked = [167, 33, 23];
let result = a.map(x => ({
...x,
active: marked.includes(x.id)
}))
console.log(result)
You can make this faster if you go over the marked
array and store its elements in an object. Then during the map
checking if element x.id
is inside the object will be faster (vs checking inside array). But in practice in most of the cases you should not notice the difference.
This is really just an extension of @Giorgi Moniava's answer since the question has been updated. I think you should mark his answer as the accepted one. Basically all you need to do is check if the value for active
is true or false. If it's true, set the status to updated
otherwise, set the status to an empty string.
let a = [
{ id : 23, active :false, status:"" },
{ id : 33, active :false, status:"" },
{ id : 167, active :false, status:"" },
{ id : 18, active :false, status:"" },
{ id : 200, active :false, status:"" },
]
let marked = [167,33,23];
let result = a.map(obj => {
const isActive = marked.includes(obj.id);
const status = isActive ? 'updated' : '';
return {
...obj,
active: isActive,
status: status
}
})
console.log(result)
Try with this, use map function with includes function. Hope so this answer gives your actual result. if not then ment to me I'll give you another answer.
const found = a.map(function(mapData){
(marked.includes(mapData.id)) ? mapData.active = true : '';
return mapData;
})