I have an array that has sub-values for each value of the array and I am trying to check if a sub-value already exists within the array. Normally I would just use array.includes()
but that isn't working in this case. How would I check for sub-values? Here's what I mean:
What I tried:
if (dataArray.includes(`classCRN: ${classesArray[i]}`) === false) {
// ...rest of code...
}
You can use the array.some() method to check it there are the same sub-values.
if(dataArray.some(item => item.classCRN == classesArray[i])){
......
}
Just loop through array to find property:
let arr = [
{name: 'test', origin: 'something', value: 'bb123'},
{country: 'Serbia', city: 'Belgrade'},
{something: 'here', hey: 'there'}
]
console.log(isInnArray(arr,'there')) //true
console.log(isInnArray(arr,'mystring')) //false
function isInnArray(arr, findMe) {
for(let i = 0; i < arr.length; i++) {
for (const [key, value] of Object.entries(arr[i])) {
if(value===findMe)
return true
}
}
return false;
}