For a array of objects, and arrays
how to check based on conditions using javascript
In the array of objects, arrobj
and obj
-
get the array of object having same
value
property andvalueid
property -
If
codevalue
andcid
value not same andcid
value includes inlistcode
return empty array of object []else return that array of object by
codevalue and cid
in javascript
var listcode =["IN","FI", "FR"];
var arrobj =[
{id:1, name: "abc", cid: "IN", value: "A1234"},
{id:2, name: "xyz", cid: "SG", value: "B3456"},
{id:3, name: "abi", cid: "SP", value: "B3456"},
{id:4, name: "ken", cid: "FI", value: "C5678"},
]
var obj={
id:5, name: "cool", codevalue: "SP", valueid:"B3456"
}
Expected Output
// codevalue and cid has same value and listcode value does not have, so fetch by value and valueid
[
{id:3, name: "abi", cid: "SP", value: "B3456"},
{id:2, name: "xyz", cid: "SG", value: "B3456"}
]
===
var listcode =["IN","FI","FR"];
var arrobj1 =[
{id:1, name: "abc", cid: "IN", value: "A1234"},
{id:2, name: "xyz", cid: "FI", value: "B3456"},
{id:3, name: "abi", cid: "IN", value: "B3456"},
{id:4, name: "ken", cid: "FI", value: "C5678"},
]
var obj1={
id:5, name: "cool", codevalue: "SP", valueid:"B3456"
}
Expected Output
// not same cid and codevalue, all cid value have listcode
[]
===
var listcode =["IN","FI","FR"];
var arrobj2 =[
{id:1, name: "abc", cid: "IN", value: "A1234"},
{id:2, name: "xyz", cid: "FI", value: "B3456"},
{id:3, name: "abi", cid: "SG", value: "B3456"},
{id:4, name: "ken", cid: "FI", value: "C5678"},
]
var obj2={
id:5, name: "cool", codevalue: "FI", valueid:"B3456"
}
Expected Output
// not same cid and codevalue, cid value dont have listcode value
[
{id:2, name: "xyz", cid: "FI", value: "B3456"},
{id:3, name: "abi", cid: "SG", value: "B3456"}
]
const result = arrobj.filter(e => e.value === obj.valueid && listcode.includes(e.cid));