When comparing an array of objects with a single object and listing the arrays in JavaScript, specific conditions need to be met to retrieve the array of objects:
If the
itemvalue
andidvalue
are the same, check if thearrobj cid
has the samecodevalue
and return both array of objects.If the
itemvalue
andidvalue
are the same, check for mismatchingcid
in the arraylist and acodevalue
that does not exist in the arraylist, then return the array of objects.If the
itemvalue
andidvalue
are the same, check if thecodevalue
matches in the arraylist and thecid
has a value that does not exist in the arraylist, return the array of objects.
If none of the above conditions are met, return an empty array []
//data1
var arraylist = ["IN","FP", "FN"];
var arrobj1 = [
{id:1, name: "sun", cid: "IN", itemvalue: "3456"},
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},
{id:3, name: "tues", cid: "SP", itemvalue: "4567"},
{id:4, name: "thurs", cid: "FI", itemvalue: "2345"},
]
var obj1 = { id:5, name: "ben", codevalue: "SG", idvalue:"4567"};
Expected Output:
[
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},
{id:3, name: "tues", cid: "SP", itemvalue: "4567"}
]
//data2
var larrylist= ["IN","FI","FR"];
var arrobj2 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "IN", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj2 = { id:5, name: "ben", codevalue: "SP", idvalue:"2468"};
Expected Output:
[]
//data3
var arraylist= ["IN","FI","FR"];
var arrobj3 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "SG", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj3 = { id:5, name: "ben", codevalue: "FI", idvalue:"2468"};
Expected Output:
[
{id:2, name: "mon", cid: "FI", itemvalue: "2468"}
]
Tried:
const result = arrobj1.filter((item) => {
return item.itemvalue === obj.idvalue &&
(
!arraylist.includes(item.cid)
|| item.cid === obj.codevalue
)
})