目前想到的方法有这么几个

1.indexOf()  -> ES5

const array = ['apple', 'banance', 'orange']

array.indexOf('apple')    // 0 存在

array.indexOf('strawBerry')     // -1不存在
 
2.find()   -> ES6
const array = ['apple', 'banance', 'orange']
array.find(obj => obj == 'banance')    //banance 存在
array.find(obj => obj == 'strawBally')    //undefined 不存在
 
const arrayList = [
  {name: '张三'}, {name: '李四'}
]
arrayList.find(obj => obj.name == '李四')        //{name: '李四'} 存在
arrayList.find(obj => obj.name == '王五')        //undefined 不存在
 
3.findIndex()  -> ES6
const array = ['apple', 'banance', 'orange']
array.findIndex(obj => obj == 'banance')    //大于0存在
array.findIndex(obj => obj == 'strawBally')    //-1 不存在
 
3.includes()
const array = ['apple', 'banance', 'orange']
array.includes('banance')      //true存在
array.includes('strawBally')      //false存在
 
4.filter()
const array = ['apple', 'banance', 'orange']
array.filter(obj => obj == 'orange')     //['orange'] 存在
array.filter(obj => obj == 'strawBally')     //[] 不存在
 
const array = [{ name: 'banance' }, { name: 'apple' }]
console.log(array.filter(obj => obj.name === 'apple'))    //[{name: 'apple'}] 存在
console.log(array.filter(obj => obj.name === 'strawBally'))    //[] 不存在
 

js在数组中查找是否存在某一个数值的更多相关文章

  1. JS在一个数组中查找某个用户输入的值,返回对应值所在索引值

    方法有很多种 第一:直接循环,判断输出 第二:使用indexOf 正常来说,为了增加工作效率一般会选择indexOf,但是indexOf存在兼容性问题,因此最完善的写法如下 function inde ...

  2. JS判断数组中的对象的每一个值不能为空

    方法一:使用every()函数,此函数不怎么常用,想要了解更多请自查 //表格 evaluateData为表格的数据 <el-table id="out-table3" :d ...

  3. [简单-剑指 Offer 53 - I. 在排序数组中查找数字 I]

    [简单-剑指 Offer 53 - I. 在排序数组中查找数字 I] 统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出 ...

  4. js从数组中删除指定值(不是指定位置)的元素

    RT: js从数组中删除指定值的元素,注意是指定值,而不是指定位置. 比如数组{1,2,3,4,5},我要删除其中的元素3,但是这个3的位置我是不知道的,只知道要删除值为3的这一个元素,请问要怎么写? ...

  5. js 删除 数组中某个元素(转载)

    来源:https://www.jb51.net/article/134312.htm js删除数组中某一项或几项的几种方法 https://www.jb51.net/article/154737.ht ...

  6. js判断数组中是否包含某个元素

    参考:http://www.runoob.com/jquery/misc-inarray.html js判断数组中是否包含某个元素 $.inArray( value, array [, fromInd ...

  7. Js判断数组中是否存在某个元素

    Js判断数组中是否存在某个元素 方法一:indexOf(item,start); Item:要查找的值:start:可选的整数参数,缺省则从起始位子开始查找. indexOf();返回元素在数组中的位 ...

  8. JS去除数组中重复值的四种方法

    JS去除数组中重复值的四种方法 1 /// <summary>            o[this[i]] = "";  }      }       newArr.p ...

  9. java语言在某个数组中查找某个字符出现的次数

    package com.llh.demo; import java.util.Scanner; /** * * @author llh * */ public class Test { /* * 在某 ...

随机推荐

  1. java操作Jacoco合并dump文件

    记录瞬间 import org.apache.maven.plugin.MojoExecutionException; import org.jacoco.core.tools.ExecFileLoa ...

  2. windows 共享文件

  3. Windows Server 2008安装IIS 7与配置

    一.安装IIS 7 1.鼠标右键[我的电脑(Computer)]→[管理(Manager)] 2.选择[角色(Roles)],右键[添加角色(Add Roles)],弹出[添加角色向导(Add Rol ...

  4. java中的getStackTrace和printStackTrace的区别

    getStackTrace()返回的是通过getOurStackTrace方法获取的StackTraceElement[]数组,而这个StackTraceElement是ERROR的每一个cause ...

  5. linux 定时任务,压缩 日志,并删除掉 指定日期之前的 日志

    sh文件 #!/bin/sh myPath="/var/www/Client/storage/logs/" myFile="lumen.log" cd $myP ...

  6. CSc 352 (Spring 2019): Assignment

    CSc 352 (Spring 2019): Assignment 11Due Date: 11:59PM Wed, May 1The purpose of this assignment is to ...

  7. webpack的常识概念

    它的优势: 递归解析依赖,支持支持es module规范.commonJS.AMD规范. 支持代码分割. loader: css-loader\style-loader\less-loader\sas ...

  8. Django路由控制

    本文目录 一 Django中路由的作用 二 简单的路由配置 三 有名分组 四 路由分发 五 反向解析 六 名称空间 七 django2.0版的path 回到目录 一 Django中路由的作用 URL配 ...

  9. centos7 安装pgsql

    1.添加prm安装源(或者从官网下载) PostgreSQL官网地址:https://yum.postgresql.org/ yum install https://download.postgres ...

  10. Bootstrap常用单词组

    布局容器 .container 固定宽度 .container-fluid 全屏 .row 行 .col-lg- 大屏幕 .col-md- 中屏幕 变量 @grid-columns: 12 列数 @g ...