I have an array of object, a object, and an arraylist.
I have to get the array of objects based on conditions using javascript:
Fetch the array of objects if the value property matches with object item
Check if the code value exists in the arraylist arraylist, if exists return array ibject
Else return empty
var arrobj1 =[
{id:1, name: zen, code: "SP", value: 100},
{id:2, name: abi, code: "IN", value: 200},
{id:3, name: lisa, code: "SG", value: 200}
]
var arraylist =["IN"]
var obj1={
id:34, code: "SP", country: "mexico", item: 200
}
Expected Output:
// value & item matches, code does not exists so empty []
var arrobj2 =[
{id:1, name: zen, code: "SP", value: 300},
{id:2, name: abi, code: "SP", value: 200},
{id:3, name: lisa, code: "SG", value: 100}
]
var arraylist =["IN"]
var obj2={
id:34, code: "SP", country: "italy", item: 100
}
Expected Output
// value & item matches, code matches
[
{id:3, name: lisa, code: "SG", value: 100}
]
I have tried using below code:
var result = arrobj1.some(e => {
if (e.value === obj.item &&
(arraylist.includes(e.code)){
return true;
}
return false;
});
First filter items, then use some.
let filtered = arrobj1.filter(({value}) => value == obj1.item)
let result = filtered.some(({code}) => arraylist.includes(code)) ? [] : filtered