I have two array of object values,
a1
and a2
if idvalue
and cidvalue
are equal,
mainid
and main
are true
then return
true
else return
false
var result = a1.find(e=> a2.find(i=>i.cidvalue===e.idvalue));
var a1=[
{id:1, idvalue: “teamA“,mainid: true, name: “ben4”}
]
var a2 =[
{id:2, cidvalue: “teamA”, main: true, name: ”ben3”},
{id:3, cidvalue: undefined, main: false, name: ”ben2”},
]
Expected Output
true
CodePudding user response:
You're code is working exactely as intended.
Edit the match clause to this particular statement:
i => i.cidvalue === e.idvalue && e.mainid === i.main
And check out this working snippet with some tweaks using Array.prototype.every below to get back a boolean value indicating if the match was found or not:
const a1 = [
{ id: 1, idvalue: "teamA", mainid: true, name: "ben4" },
]
const a2 = [
{ id: 2, cidvalue: "teamA", main: true, name: "ben3" },
{ id: 3, cuid: undefined, main: false, name: "ben2" },
]
const result = a1.every(e => a2.find(i => i.cidvalue === e.idvalue && e.mainid === i.main));
console.log(result)
CodePudding user response:
here, I have list a2 with a1 and set if the data is more than 0 then true.
var a1 = [
{ id: 1, idvalue: "teamA", mainid: true, name: "ben4" }
]
var a2 = [
{ id: 2, cidvalue: "teamA", main: true, name: "ben3" },
{ id: 3, cuid: undefined, main: false, name: "ben2" },
]
console.log(a2.filter(item => a1.find(itemm => itemm.idvalue === item.cidvalue)).length > 0);
CodePudding user response:
Use Array.every
to compare each elements in array a1
against a2
var a1 = [
{ id: 1, idvalue: "teamA", mainid: true, name: "ben4" },
]
var a2 = [
{ id: 2, cidvalue: "teamA", main: true, name: "ben3" },
{ id: 3, cuid: undefined, main: false, name: "ben2" },
]
var result = a1.every(e => a2.find(i => i.cidvalue === e.idvalue && e.mainid && i.main));
console.log(result);