I would like to know how to create a new array object if the key exists in JavaScript.
I have two array objects, arr1 and arr2. In arr2, if the c value is true and the key of arr1 and a value are equal, push it to a new array object. If arr2 of key-value is true and matches with the key of arr1, then push to a new array in JavaScript.
var arr1 = {
data:[
{id:1, place: "IN", year: "2020", mode: "ON"},
{id:3, place: "TH", year: "2022", mode: "OFF"},
{id:5, place: "AU", year: "2025", mode: "ON"}
]
};
var arr2=[
{a: "place", c: true},
{a: "year", c: true},
{a: "mode", c: false},
]
var finals = [];
var result = arr1.data.map(e=>{
arr2.forEach(i=>{
if(i.c == true && Object.keys(e)){
finals.push(e);
}
})
return finals
})
Expected Output:
[
{place: "IN", year: "2020"}
{place: "TH", year: "2022"},
{place: "AU", year: "2025"}
]