6
I was solving some challenges of a Javascript course I’m doing, and came across a small problem:
I have the following object array:
var numberObjects = [
{number: 1},
{number: 2},
{number: 3},
{number: 4},
{number: 5},
{number: 6},
{number: 7},
{number: 8},
{number: 9},
{number: 10}
];
And I need to check for the object {number: 2}
within this array, using the method indexOf()
and return a message on the console if it is true
or false
, then I did as follows using the ternary operator:
console.log( numberObjects.indexOf( { number: 2 } ) > -1 ? 'Existe um objeto { number: 2 } em numberObjects!' : 'Não existe um objeto { number: 2 } em numberObjects :(' );
My problem is that even though the object exists { number: 2 }
within my array, the return of indexOf()
continues to be -1
, which means that he is not finding that object within my array, someone would know to inform me why this happens?
I also tried to do by assigning the return of indexOf()
a variable, however, without success.
Welcome to Stackoverflow in English. I edited your question to remove the greetings as we usually keep the text as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the [chat]. If you have questions about the operation, rules and procedures of the site visit the [meta] :)
– Icaro Martins