For the array of object, compare with object and list array using javascript
should get the array of objects based on below conditions
itemvalue and idvalue same, from that checkarrobj cid has same codevaluereturn both array of objectsitemvalue and idvalue are same, from that check ifcid not matching the arraylistandcodevalue has value that does not exist in arraylist, return array of objectsitemvalue and idvalue same, from that check ifcodevalue matching the arraylistandcid has value that does not exist in arraylist, return array of objects
return empty array [] if above fails
//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
[]
***
//data2
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
)
})
arrobjdoesn't have acodevalueproperty. What does "codevalue not matching the arraylist" mean?codevalueis a string. How could it possibly match the arrayarraylist? Please continue your efforts to express the conditions more precisely.