Hi !
I want to run this code using the method hasOwnProperty() but it seems like it’s not working ! what is wrong in that code ?
does the method hasOwnProperty workis within arrays or something else
can anyone explain that to me ?
I’ll be so thankful if someone answers my question… Your code so far
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for(let i=0;i<arr.length;i++){
if(arr[i].hasOwnProperty(elem){
//or we can use indexOf//
newArr.push(arr[i]);
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36
Challenge: Iterate Through All an Array’s Items Using For Loops
hasOwnProperty is object method. You work with arrays. This means you can only use array methods. Methods which can help you in that case can be indexOf, includes and prolly some others i cannot recall atm.
So arr is an array that contains nested arrays. On the first pass through your for loop, arr[0]===[3,2,3].
But hasOwnProperty is a function useful with objects, not arrays. It will tell you if a given object has a property name matching the strung you’ve passed.
In the case of arrays, which are simply an ordered list of values, there are no property names. But i think i know what you were trying to do, and arrays do have a handy function for it.
Take a look at the docs for array.includes: DevDocs, see if that might apply.