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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值