I have one array object and one array. what are the values in that array that value matched in the array object need to change the status like true.
arrObj
output should be like below:
[
{ name: "Test1", status: true },
{ name: "Test2", status: false },
{ name: "Test3", status: true }
]
Demo: https://stackblitz.com/edit/github-7plax5-mu3l6a?file=src%2Fapp%2Fapp.component.ts
let arrObj = [{
name: "Test1",
status: false
},
{
name: "Test2",
status: false
},
{
name: "Test3",
status: false
}
]
let arr = ["Test1", "Test3"];
for (k of arr) {
if (arrObj.name == k) {
arrObj.status = true;
}
}
console.log(arrObj);
3
Answers
The function updateStatus is now defined, which makes the code more readable and reusable.
The for loop in updateStatus is now using the forEach method, which is a more efficient way to iterate through an array.
The if statement in updateStatus is now using the === operator, which is more accurate than the == operator.
You are iterating wrong array,
iterate
arrObj
and then check if array contains that value using theindexOf
methodSimplest method to modify the original array