I’ve below code snippet
const arr = [
{
"name": "Attestation Component 1",
"values": [
{
"component": "Attestation Component 1"
},
{
"component": "Attestation Component 1"
},
{
"component": "Attestation Component 1",
}
]
},
{
"name": "Attestation Component 2",
"values": [
{
"id": "10005884",
"url": "https://www.msn.com",
"bfaId": "G44.5.3.1N/A",
"component": "Attestation Component 2"
},
{
"id": "10005883",
"url": "https://www.hotmail.com",
"bfaId": "G44.5.3.2N/A",
"component": "Attestation Component 2"
}
]
},
{
"name": "Attestation Component 3",
"values": [
{
"id": "10005882",
"url": "https://www.rediffmail.com",
"bfaId": "G44.5.3.3N/A",
"component": "Attestation Component 3"
}
]
}
]
const bool = arr.map(group => group.values.every(val => val.id));
console.log(bool);
I’ve three object with name Attestation Component 1, Attestation Component 2 ,Attestation Component 3. I’m getting the expected output as false, true, true. What’s the reason for this? I want to add the property to the existing array of object as something isInvalid: true/false
below name
Expected O/P
isInvalid: true/false
3
Answers
That is because your algorithm is incorrect. The
every
method will check if all the objects have an id, but that is not what you want right ?So try this instead
Instead of every(), you should use some().
every() method is used to check whether all the elements of the array satisfy the given condition or not. The Array. some() method is used to check whether at least one of the elements of the array satisfies the given condition or not.
Check if there is at least one object in the
arr
array that has a value with the property namedid
using the.some()
method.