简体   繁体   English

如何检查某个值是否存在于将对象作为其元素的数组中。 JavaScript

[英]How to check if a certain value exist in an array which holds objects as its elements. JavaScript

The first one is when we have an array holds regular elements like strings and I know how to use Includes on it.第一个是当我们有一个数组包含字符串等常规元素并且我知道如何在其上使用 Includes 时。

But how about arrays holding objects as their element?但是 arrays 将对象作为元素呢? How can I check if there is a certain value in those objects?我如何检查这些对象中是否有特定值?

 const arr = ['this', 'is', 'a test']; console.log(arr.includes('this')); const arr2 = [ { id: '123', name: 'Alex' }, { id: '345', name: 'Dan' }, { id: '33', name: 'Joe' }, ];

You can use the some function.您可以使用some function。

const arr2 = [
  { id: '123', name: 'Alex' },
  { id: '345', name: 'Dan' },
  { id: '33', name: 'Joe' },
];

console.log(arr2.some(item => item.name==="this" ))

The some() method tests whether at least one element in the array passes the test implemented by the provided function. some()方法测试数组中是否至少有一个元素通过提供的 function 实现的测试。

if you want a returned array of values that match = >如果你想要一个返回的值数组匹配=>

 const filtered = arr2.filter((item)=>item.name == 'value')

if you want bool(true||false) =>如果你想要 bool(true||false) =>

 const filtered = arr2.filter((item)=>item.name == 'value').length > 0

or using some as mentioned above.或使用上面提到的一些。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回过滤掉 javascript 中数组值中不存在的对象数组 - How to return filter out array of objects which is not exist in array value in javascript 如何检查json数组中是否存在值javascript - How to check if value exist in a json array javascript 如何避免包含相同对象的两个数组的反射? - How to avoid reflection of two array which holds the same objects? 如何检查javascript数组是否持有具有特定值的属性,如果是,则返回true - How to check if javascript array holds property with specific value, if so returns true 如何通过其值将某些键查找到混合对象数组中? - how to find certain key by its value into mixed objects array? 如何检查 JavaScript 对象数组中是否存在特定对象? - How do I check if a particular object exist or not in a JavaScript array of objects? 如何根据JavaScript中的值检查对象是否在数组中? - How to check if objects are in an array based on a value in JavaScript? 我有一个对象数组,我想检查这个数组中是否存在一个元素 javascript - i have an array of objects i wanna check if an elements exist in this array javascript javascript 数组显示元素,元素之间使用逗号。 如何将逗号更改为破折号 - javascript array display elements with a comma between elements. How do I change the comma to a dash Javascript过滤来自数组的元素,它将元素保存在另一个数组中 - Javascript filter elements from array which holds elements together from another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM