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

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:tomcat(负载均衡)nginx的应用配置

    http://mini.eastday.com/mobile/180627012211514.html 1.什么是负载均衡 由于系统各个核心部分随着业务量的提高,访问量和数据流量的快速增长,单一的服务 ...

  2. 洛谷P2611 信息传递

    并查集裸题,记录每个点的胜读,取个min就好了 #include<stdio.h> #include<string.h> #include<algorithm> u ...

  3. mysql 5.7 配置文件说明

    1.配置文件样例 [client] #password= socket=/data/var/mysql/mysql.sock [mysqld_safe] pid-file=/data/var/mysq ...

  4. 7zip 18.3性能测试

    配置 7zip 18.3 bate3 的性能大幅提升,进行一点简单测试 配置为 E3 1230v2 3.5G 4核心/8线程 12G内存 均为8线程测试 压缩解压一个1.5G的虚拟机磁盘镜像 测试 再 ...

  5. Flask实战-留言板-使用Bootstrap-Flask简化页面编写

    使用Bootstrap-Flask简化页面编写 扩展Bootstrap-Flask内置了可以快速渲染Bootstrap样式HTML组件的宏,并提供了内置的Bootstap资源,方便快速开发,使用它可以 ...

  6. js'基础-1

    ---恢复内容开始--- ----------- 1.return <!DOCTYPE html><html lang="en"><head>  ...

  7. node离线版安装

    1.下载 下载地址:https://nodejs.org/zh-cn/download/ 选择相应的版本下载 2.解压缩 将文件解压到要安装的位置,并新建两个目录 node-global :npm全局 ...

  8. 向日葵连CentOS

    TeamViewer可以连CentOS,但TeamViewer最近老是提示“检测为商业用途”,5分钟就会自动断,用起来非常不爽,所以决定改用向日葵试下,向日葵目前的口碑也不错,更何况是国产软件,更要支 ...

  9. AOP之proceedingjoinpoint和joinpoint区别(获取各对象备忘)、动态代理机制及获取原理代理对象、获取Mybatis Mapper接口原始对象

    现在AOP的场景越来越多,所以我们有必要理解下和AOP相关的一些概念和机制. import org.aspectj.lang.reflect.SourceLocation; public interf ...

  10. algs4 使用 DrJava 编写 Hello World on Windows

    前阶段读了 Yinwang 的博客, 对 Scheme and Lisp 产生了很大的兴趣, 用 学生模式的  DrRacket IDE一步一步开始实现 How to Design Programs. ...