I've two arrays like below
flag: true
firstName: "user1"
lastName: "usr1"
flag: true
firstName: "user2"
lastName: "usr2"
flag: true
firstName: "user3"
lastName: "usr3"
And the other one like below
"user1"
"user2"
My intention is, the second array value matches the first array with firstname then needs to override flag with false.
I'm expecting a result like
flag: false
firstName: "user1"
lastName: "usr1"
flag: false
firstName: "user2"
lastName: "usr2"
flag: true
firstName: "user3"
lastName: "usr3"
Any good way to achieve this in react, Thanks
CodePudding user response:
consider:
array 1 = users
array 2 = userNames
Then:
users.map((user) => ({...user, flag: !userNames.includes(user.firstName)}))
This will return you the new array
CodePudding user response:
arr1.map((item, index) => {
const newItem = {...item};
if (item.name === arr2[index].name) {
newItem.flag = false;
}
return newItem
});
This loops through the array, and returns it with each item (object) in it with the flag property adjusted.