I'm trying to add a property with a certain value to all of the objects in one array, based on a corresponding value in another array.
const array1 = [
{
id: 1,
date: '2022.05.01',
name: 'john'
}, {
id: 2,
date: '2022.05.01',
name: 'sam'
}, {
id: 3,
date: '2022.05.03',
name: 'john'
}, {
id: 4,
date: '2022.05.06',
name: 'jack'
},
]
This array contains the required modifications that need to be made:
const array2 = [
{
name: 'john',
isCanceled: true,
}, {
name: 'jack',
isCanceled: false,
}, {
name: 'sam',
isCanceled: false,
},
]
If the name in the object within array1
is john
then isCanceled
should be set to true
, but if it's jack
or sam
it should be set to false
like so:
const resultArray = [
{
id: 1,
date: '2022.05.01',
name: 'john',
isCanceled: true,
}, {
id: 2,
date: '2022.05.01',
name: 'sam'
isCanceled: false,
}, {
id: 3,
date: '2022.05.03',
name: 'john'
isCanceled: true,
}, {
id: 4,
date: '2022.05.06',
name: 'jack'
isCanceled: false,
},
];
Build a status
lookup object containing the status, and map your array to include the matching values from the lookup object:
const status = Object.fromEntries(
array2.map(({name, isCanceled}) => [name, isCanceled])
);
const resultArray = array1.map(({id, date, name}) => ({
id,
date,
name,
isCanceled: status[name]
}));
Alternatively, if you just want to modify array1
instead of creating a new array, the second step can be replaced with:
array1.forEach(v => v.isCanceled = status[v.name]);
Complete snippet:
const array1 = [{ id: 1, date: '2022.05.01', name: 'john' }, { id: 2, date: '2022.05.01', name: 'sam' }, { id: 3, date: '2022.05.03', name: 'john' }, { id: 4, date: '2022.05.06', name: 'jack' }, ]; const array2 = [{ name: 'john', isCanceled: true, }, { name: 'jack', isCanceled: false, }, { name: 'sam', isCanceled: false, }, ]; const status = Object.fromEntries( array2.map(({name, isCanceled}) => [name, isCanceled]) ); const resultArray = array1.map(({id, date, name}) => ({ id, date, name, isCanceled: status[name] })); console.log(resultArray);
Should you wonder why I prefer to build the lookup object instead of using find()
inside the map()
operation: in terms of complexity, the lookup table has a complexity of O(1) , while the find()
approach is O(n) .
you can do something as :
const result = array1.map(person => {
const lookPerson = array2.find(person2 => person2.name === person.name);
return (lookPerson) ? {
...person,
isCanceled: lookPerson.isCanceled
} : person;
});
combine array.map to transform array1
use array.find to get first iteration of similar person between array1 and array2
merge object with
{ ...person, isCanceled: lookPerson.isCanceled }
const array1 = [{ id: 1, date: '2022.05.01', name: 'john' }, { id: 2, date: '2022.05.01', name: 'sam' }, { id: 3, date: '2022.05.03', name: 'john' }, { id: 4, date: '2022.05.06', name: 'jack' }, ]; const array2 = [{ name: 'john', isCanceled: true, }, { name: 'jack', isCanceled: false, }, { name: 'sam', isCanceled: false, }, ]; const result = array1.map(person => { const lookPerson = array2.find(person2 => person2.name === person.name); return (lookPerson) ? { ...person, isCanceled: lookPerson.isCanceled } : person; }); console.log(result);
You can do this more simply by first converting your array2
into an object and then using a lookup within a map
like so:
const array1 = [{id:1,date:"2022.05.01",name:"john"},{id:2,date:"2022.05.01",name:"sam"},{id:3,date:"2022.05.03",name:"john"},{id:4,date:"2022.05.06",name:"jack"},]; const array2 = [{name:"john",isCanceled:true},{name:"jack",isCanceled:false},{name:"sam",isCanceled:false}]; const arrCancel = array2.reduce((a, { name, isCanceled }) => { a[name] = isCanceled; return a; }, {}); const resultArr = array1.map(e => { e.isCanceled = arrCancel[e.name]; return e; }); console.log(resultArr);
.as-console-wrapper { max-height: 100% !important; top: auto; }
You can do:
const array1 = [{id: 1,date: '2022.05.01',name: 'john'}, {id: 2,date: '2022.05.01',name: 'sam'}, {id: 3,date: '2022.05.03',name: 'john'}, {id: 4,date: '2022.05.06',name: 'jack'}] const array2 = [{name: 'john',isCanceled: true,}, {name: 'jack',isCanceled: false,}, {name: 'sam',isCanceled: false,}] const array2Hash = array2.reduce((a, { name: n, isCanceled: ic}) => (a[n] = ic, a), {}) const result = array1.map(u => ({ ...u, isCanceled: array2Hash[u.name] })) console.log(result)
resultArray is map of array1
with addition isCanceled
based of array2
:
resultArray = array1.map( a => Object.assign(
{},
a,
{isCanceled: array2.find(b => a.name == b.name)?.isCanceled||false}) )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.