1

My array is

arrays = [1, 2, 3]

index for array is

arrays = [ 0:1 , 1:2, 2:3  ]

my variable is

id = 3

I have to check if 3 is in array and 3 is in the index value of 2.

I am using for loop

for (let z in this.arrays){
    if (id in this.arrays[z]){
        console.log("duplicate")
    }
}

but I got an error

ERROR TypeError: Cannot use 'in' operator to search for '2' in 1

How to do that using javascript

6
  • I guess this.array[z] is not the array Commented Feb 27, 2021 at 6:13
  • 2
    You're checking if id is a key (in an array an index) of an element in your array when you use in on this.array[z]. You're essentially asking "is 3 a key in the number 1?", which doesn't really make sense since 1 is a primitive and not an object - use .includes() if you want to check if a value is in an array. Commented Feb 27, 2021 at 6:16
  • 2
    That "arrays" thing is not syntactically correct. Commented Feb 27, 2021 at 6:33
  • I am trying to display with index value but actual array is arrays=[1, 2, 3 ] @ Pointy Commented Feb 27, 2021 at 6:43
  • You should use for (let .. of ..) to iterate over arrays. Commented Feb 27, 2021 at 6:48

3 Answers 3

2

You need to understand about "in" operator first. The in operator returns true if the specified property is in the specified object or its prototype chain or in an Array. How to use this for Array (sample reference here ):

// Arrays 
let trees = ['redwood', 'bay', 'cedar', 'oak', 'maple']
0 in trees        // returns true
3 in trees        // returns true
6 in trees        // returns false
'bay' in trees    // returns false (you must specify the index number, not the value at that index)
'length' in trees // returns true (length is an Array property)
Symbol.iterator in trees // returns true (arrays are iterable, works only in ES2015+)

// Predefined objects
'PI' in Math          // returns true

// Custom objects
let mycar = {make: 'Honda', model: 'Accord', year: 1998}
'make' in mycar  // returns true
'model' in mycar // returns true

What you're checking on Array is the key, which is its index. You should use find method in array to find any values instead of "in":

const arrays=[ 1 , 2, 3 ];
const arrays_1=[ 1 , 2, 3, 3, 4, 3 ];
const found = arrays.find(el => el === 3); // it will return the first value if it matches the condition.
const found_all = arrays_1.filter(el => el === 3); // will give all the matching values.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you are trying to use the in operator on an array value instead of on the array itself.

Do something like this:

for( let z in arrays){
 if( 2 in arrays && z == 2 && arrays[z] == 3) console.log("duplicate");
}

Check the following link for an in depth explanation on the “in” operator:

https://www.google.com/amp/s/www.freecodecamp.org/news/the-javascript-in-operator-explained-with-examples/amp/

Comments

-1
id in this.arrays[z]

because arrays[z] must be array or object. In checks for key existence

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.