Home  >  Article  >  Web Front-end  >  How to determine whether an element exists in a javascript array

How to determine whether an element exists in a javascript array

青灯夜游
青灯夜游Original
2021-04-27 17:05:5816446browse

Judgment method: 1. Use indexOf() method, syntax "arr.indexOf(value to be found)"; 2. Use "arr.find()" method; 3. Use "array.findIndex( )" method; 4. Use the "$.inArray('value to be found', arr)" method.

How to determine whether an element exists in a javascript array

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Js determines whether an element exists in the array

Method 1: indexOf(item,start);

  • Item: The value to be searched;

  • start: Optional integer parameter, the default is to start searching from the starting position.

indexOf(); Returns the position of the element in the array, if not, returns -1;

Example: var arr=['aaa','bbb' ,'ccc','ddd','eee'];

  var a=arr.indexOf('ddd');
  console.log(a);   //3
  var b=arr.indexOf('d');
  console.log(b);  //-1

My usual usage: if(arr.indexOf(element to be found)>-1){Operation for element existence};

indexOf() cannot find NaN

Method 2: arr.find();

The parameter of Arr.find() is a callback function. All elements of the array will traverse this callback function until the first element with a return value of true is found, and then return the element, otherwise return undefined;

  var arr=['aaa','bbb','ccc','ddd','eee'];
  var a=arr.find(function(value,index,arr){
    return value=='bbb';
  })
  console.log(a);  //bbb,这里返回的不是true,而是返回值为true的这个元素;

My usual usage:

  arr.find(function(value){
    If(value==要查找的值){
      //所做的操作
    }
  })

Method 3: array.findIndex();

findIndex() and find() are similar in usage. find() returns the element, and findIndex returns the position of the element. findIndex(); returns the position of the first array element that meets the conditions. If all elements do not meet the conditions, it returns -1; findIndex(), each element in the array will call the function once, but when the condition returns true, findIndex() returns the position of the element that meets the conditions, and the execution function will not be called for subsequent values.

  var arr=['aaa','bbb','ccc','ddd','eee'];
  var a=arr.find(function(value,index,arr){
    return value=='bbb';
  })
  console.log(a);//1,后面的值不会再调用函数。

Description: findIndex() and find() can be used to find NaN;

  var arr=['1','2','3',NaN];
  var a=arr.find(function(value){
    return isNaN(value);
  })
  console.log(a);       //NaN

Method 4:

Use jquery’s inArray method, This method returns the subscript of the element in the array. If it does not exist in the array, then returns -1;

  var arr=['aaa','bbb','ccc','ddd','eee'];
  var a= $.inArray('bbb',arr);
  console.log(a);    //1

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to determine whether an element exists in a javascript array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn