In this article, we will look at various methods to check if an array includes an object in JavaScript. Consider the following object,

在本文中,我们将研究各种方法来检查数组是否包含JavaScript中的对象 。 考虑以下对象,

const squirtle= {name: 'Squirtle-1',
HP: 100,
type: 'water',
favSnack: 'Choco balls'
}
const pokeArray=['name','location','type']
console.log(squirtle);
console.log(pokeArray);

Output

输出量

{name: "Squirtle-1", HP: 100, type: "water", favSnack: "Choco balls"}
(3) ["name", "location", "type"]

The first method we can use is the includes() method to check if an array contains a property. We can call this method on an array and pass in two parameters, the object name and starting position.

我们可以使用的第一个方法是include()方法,以检查数组是否包含属性。 我们可以在数组上调用此方法,并传入两个参数,即对象名称和起始位置。

console.log(pokeArray.includes(squirtle,0))

Output

输出量

false

The pokeArray array doesn't contain the squirtle object hence it returned "false". The meaning that an array includes an object implies that we're checking inside an array that contains the name of objects and not just properties. So let's create an array of Pokemon,

pokeArray数组不包含squirtle对象,因此返回“ false”。 数组包含对象的含义意味着我们正在检查包含对象名称而不只是属性的数组内部。 因此,让我们创建一个口袋妖怪数组,

const pokemons = ["charmander", squirtle, "pikachu"];
console.log(pokemons.includes(squirtle, 0));

Output

输出量

true

Now we get true! Let's change our array a bit,

现在我们实现了! 让我们稍微改变一下数组

const list=["charmander",
"pikachu",
{name: 'Squirtle-1', HP: 100, type: 'water', favSnack: 'Choco balls'
}
]
console.log(list);

Output

输出量

(3) ["charmander", "pikachu", {…}]
0: "charmander"
1: "pikachu"
2:
HP: 100
favSnack: "Choco balls"
name: "Squirtle-1"
type: "water"
__proto__: Object
length: 3
__proto__: Array(0)

Our list array contains the whole object inside it. Now we can't use includes() because we don't know the name of the object. We can simply loop through our array and check for each element's type and return true if it was an object.

我们的列表数组包含其中的整个对象。 现在我们不能使用include(),因为我们不知道对象的名称。 我们可以简单地遍历数组并检查每个元素的类型,如果它是对象则返回true。

list.forEach(l => {if (typeof l == 'object')
console.log('Its an object!');
});

Output

输出量

Its an object!

We can simplify the above forEach loop by using some() method instead.

我们可以改为使用some()方法来简化上述forEach循环

list.some(value => {return typeof value == 'object';
});

Output

输出量

true

Let's try a few more examples...

让我们再尝试一些示例...

const cars=['merc','ferrari',{name: 'Audi'},'tesla']
cars.some(car=>{return typeof car=='object';});

Output

输出量

true

Our cars array represents the names of car brands however it has an object inside it. Sometimes your array might contain objects which shouldn't have been one since they represent the same information that other elements of the array. In those cases, identifying if your array contains an object and then making the data structure consistent would be useful. We could also capture that object and use it somewhere else.

我们的汽车数组代表汽车品牌的名称,但是其中包含一个对象。 有时,数组可能包含不应该是一个的对象,因为它们表示的信息与数组的其他元素相同。 在这些情况下,识别数组是否包含对象,然后使数据结构一致将很有用。 我们还可以捕获该对象并在其他地方使用它。

const colors=['red','orange','blue',{date:'12th December',
day:'Thursday',
year: 2019
}
]
var Date;
colors.forEach(color=>{if(typeof color=='object')
Date=color;
})
console.log(Date);

Output

输出量

{date: "12th December", day: "Thursday", year: 2019}

In the above example, our colors array had a date object which made no sense so we captured the date object in another variable. This could be useful when you're getting loads of unkempt data from a server and you want to conditionally segregate your data based on what it represents and it's data type.

在上面的例子中,我们的颜色阵列有所以我们捕获在另一个变量的时间对象,它是没有意义的日期的对象。 当您从服务器上获取大量不受欢迎的数据,并且想要根据其表示的内容和数据类型有条件地隔离数据时,这可能很有用。

翻译自: https://www.includehelp.com/code-snippets/how-to-check-if-an-array-includes-an-object-in-javascript.aspx

如何检查数组是否包含JavaScript中的对象?相关推荐

  1. 检查值是否是JavaScript中的对象

    如何检查值是否是JavaScript中的Object? #1楼 尝试这个 if (objectName instanceof Object == false) {alert('Not an objec ...

  2. JavaScript 中检查数组是否包含值的 5 种方法

    在 JavaScript 中,有多种方法可以检查数组是否包含项目.您始终可以使用for 循环或Array.indexOf()方法,但 ES6 添加了许多更有用的方法来搜索数组并轻松找到您要查找的内容. ...

  3. 如何检查变量是否是JavaScript中的数组? [重复]

    本文翻译自:How do you check if a variable is an array in JavaScript? [duplicate] This question already ha ...

  4. 如何检查字符串是否包含Ruby中的子字符串?

    我有一个字符串变量,内容如下: varMessage = "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n""/my/name/is/balaji.s ...

  5. 如何从JavaScript中的对象数组中获得不同的值?

    本文翻译自:How to get distinct values from an array of objects in JavaScript? Assuming I have the followi ...

  6. name optimize is not defined 怎么解决_Web前端:怎么在JavaScript中比较对象?

    大家好,我来了,本期为大家带来的前端开发知识是"Web前端:怎么在JavaScript中比较对象?",有兴趣做前端的朋友,一起来看看吧! 比较JavaScript中的原始值很简单. ...

  7. 如何遍历JavaScript中的对象

    Once in a while, you may need to loop through objects in JavaScript. The only way to do so before ES ...

  8. JavaScript 中的对象拷贝(深拷贝、浅拷贝)

    对象是 JavaScript 的基本块.对象是属性的集合,属性是键值对.JavaScript 中的几乎所有对象都是位于原型链顶部 Object 的实例. 介绍 如你所知,赋值运算符不会创建一个对象的副 ...

  9. JavaScript中Object对象方法超详细讲解举例说明仅此一篇

    JavaScript中Object对象方法超详细讲解举例说明仅此一篇 Object.assign() Object.create() Object.values() Object.entries() ...

最新文章

  1. 2022-2028年中国散热产业深度调研及投资前景预测报告(全卷)
  2. android 自动打开第三方应用程序,Android如何做到应用程序图标隐藏,由第三方程序显示启动...
  3. c3p0 参数 模糊查询_mybatis之动态sql,模糊查询,结果集处理,mybatis分页及特殊字符处理...
  4. Dijkstra算法的粗略学习
  5. LVM---逻辑盘卷管理
  6. leetcode:Majority Number
  7. 什么是BETA,RC,ALPHA版 - 软件命名规范
  8. 真正优秀的人是如何度过假期的
  9. Linux下Oracle进程CPU使用率过高处理方式
  10. ESP32|基于ESP32制作的低成本、可拓展性高的NES游戏机(1)(开源ESP32 NES模拟器)-效果演示及介绍
  11. NShape(开源矢量图形编辑器) 入门(三)
  12. Jmeter线程组之jp@gc - Stepping Thread Group
  13. thinkDifferent思维糖果
  14. XY轴控制板,料盒控制板,水路流量控制板,直流电机控制方案和程序代码
  15. 局域网联机游戏找不到服务器,N2N组建虚拟局域网联机遇到搜不到房间的问题一例...
  16. 上海海事大学本科毕业论文答辩和论文选题PPT模板
  17. Windows控制面板命令大全
  18. 海康工业相机SDK + OpenCV实例(4):相机参数设置详解
  19. python 读取txt文件、转为json文件_使用python操作json文本文件
  20. 阻塞非阻塞与同步异步的区别?

热门文章

  1. java两个字符串前缀_java – 找到两个字符串的最长公共前缀
  2. mysql判断数字的函数_Mysql必读MySql判断汉字、日期、数字的具体函数
  3. 浅谈.Net版(C#)的CMP模式
  4. 统计一个panel中lable的个数
  5. BZOJ1706奶牛接力跑
  6. vue笔记(三)生命周期、组件(嵌套)、数据传递
  7. js 实现简单的轮询
  8. 电话圈(floyd)
  9. 脑子越来越不好使,文字越来越像驮shi
  10. Ferguson游戏