Reputation: 6868
Below is my main array:
var myArray = [
{field: 'Saab', flag: false},
{field: 'cStatus', flag: false},
{field: 'BMW', flag: false},
{field: 'Mercedes', flag: false}
];
var cars1 = ["Saab", "Bmw"];
I am trying to set property flag
to true of myArray
objects, whose field
property is included in the cars1
variable.
Expected output in myArray
variable only:
myArray = [
{field: 'Saab', flag: true},
{field: 'Volvo', flag: false},
{field: 'BMW', flag: true},
{field: 'Mercedes', flag: false}
];
Can anybody please show me how to do this? I've tried the following:
var myArray = [
{field: 'Saab', flag: false},
{field: 'Volvo', flag: false},
{field: 'BMW', flag: false},
{field: 'Mercedes', flag: false}
];
var cars1 = ["Saab", "BMW"];
console.log(myArray.filter(x => cars1.indexOf(x.field) > -1));
Upvotes: 0
Views: 73
Reputation: 57944
You can utilize Array.prototype.indexOf
and Array.prototype.forEach
to iterate over the array and check if cars1
contains field
:
var myArray = [
{field: 'Saab', flag: false},
{field: 'Volvo', flag: false},
{field: 'BMW', flag: false},
{field: 'Mercedes', flag: false}
];
var cars1 = ["Saab", "BMW"];
myArray.forEach(car => car.flag = cars1.indexOf(car.field) > -1);
console.log(myArray);
The above iterates over the array and car
represents the current object. Then we assign car.flag
to the result of cars1.indexOf(car.field) > -1
. The result will be true if cars1
contains the field, and false if it does not.
You can also use Array.prototype.includes
to check if the array includes a certain value, though it's not supported on all browsers (most do). This will get rid of the indexOf
check:
var myArray = [
{field: 'Saab', flag: false},
{field: 'Volvo', flag: false},
{field: 'BMW', flag: false},
{field: 'Mercedes', flag: false}
];
var cars1 = ["Saab", "BMW"];
myArray.forEach(car => car.flag = cars1.includes(car.field));
console.log(myArray);
Upvotes: 1
Reputation: 386560
You could use Set
for the cars and iterate then only myArray
.
The key part is a callback which uses a closure over a set with the content of cars1
myArray.forEach(
(cars => o => cars.has(o.field) && (o.flag = true))(new Set(cars1))
);
which basically is in the first step
(cars => o => cars.has(o.field) && (o.flag = true))(new Set(cars1))
(cars => )(new Set(cars1))
// ^^^^ closure over
// ^^^^^^^^^^^^^^ set with car values
Inside of the returned function a check is made, if field
is in the set and then the assignment of true
to o.flag
takes place.
o => cars.has(o.field) && (o.flag = true)
// ^ currentValue of array
// ^^^^^^^^^^^^^^^^^ check if field is in set
// ^^ if true
// ^^^^^^^^^^^^^^^ evaluate expression
// assign true to o.flag
var myArray = [{ field: 'Saab', flag: false }, { field: 'cStatus', flag: false }, { field: 'BMW', flag: false }, { field: 'Mercedes', flag: false }],
cars1 = ["Saab", "Bmw"];
myArray.forEach((cars => o => cars.has(o.field) && (o.flag = true))(new Set(cars1)));
console.log(myArray);
Upvotes: 1
Reputation: 188
Hey Learning you can use the forEach method to loop through your 'myArray' and then use the .find method on cars1 for each iteration through your array. If cars1.find, finds a that equals the current cars field, then it will set that value.flag to true. If it does not, it will set it to false.
Here is a link to the .find() method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
I hope this helps!
var myArray = [
{field: 'Saab', flag: false},
{field: 'Volvo', flag: false},
{field: 'BMW', flag: false},
{field: 'Mercedes', flag: false}
];
var cars1 = ["Saab", "BMW"];
myArray.forEach(value => {
cars1.find((v) => v === value.field) ? value.flag = true : value.flag = false;
})
Upvotes: 1
Reputation: 41893
One of the ways how to solve it, is to use Array#some
to check if specified element from the myArray
exists in the cars1
array. If it does - set flag
to true.
var myArray=[{field:"Saab",flag:false},{field:"cStatus",flag:false},{field:"BMW",flag:false},{field:"Mercedes",flag:false}],
cars1 = ["Saab", "BMW"];
myArray.forEach(v => cars1.some(c => c == v.field ? v.flag = true : null));
console.log(myArray);
Upvotes: 1