Reputation: 59
I have a Typescript project where I need to know if the value of a variable is in any property of the Object.
This is the object:
let listDump = [
{
"properties":{
"title":"CARS"
}
},
{
"properties":{
"title":"HOME"
}
},
{
"properties":{
"title":"COUNTRY"
}
}
];
This is the declared variable:
let newData = 'ANIMALS'
This is what I do to check if it exists:
for (let sheet of listDump) {
if (sheet.properties.title == newData) {
console.log(`do not create property`)
} else {
console.log('create property')
}
}
Problem: Doing three checks operates three times on the else
What I need to know is how to check that it exists without needing to iterate over the object and operate only once in case it doesn't exist
Upvotes: 0
Views: 790
Reputation: 4194
You can use <Array>.some
listDump.some(sheet => sheet.properties.title == newData) // returns true | false
Upvotes: 3