我有一个像这样的数组:
let myarr = [
{type:"a", class:"x", value:"p"},
{type:"b", class:"x", value:"r"},
{type:"a", class:"y", value:"p"}
];
我需要使用Array.includes
来检查带有class=y
的对象是否在此数组中。
我需要使用myarr.includes({condition});
我可以简单地比较整个对象,但是我不知道如何使用对象的键进行检查。
答案 0 :(得分:4)
为此,您可以使用some()
方法。
some()方法测试数组中的至少一个元素是否通过了由提供的函数实现的测试。
let myarr = [
{type: "a",class: "x",value: "p"},
{type: "b",class: "x",value: "r"},
{type: "a",class: "y",value: "p"}
];
let pass = myarr.some(item => item.class == 'y');
let fail = myarr.some(item => item.class == 'z');
console.log(pass);
console.log(fail);
答案 1 :(得分:2)
Array.prototype.includes
依赖于身份检查,该检查要求您传入相同的对象(不仅仅是具有相同属性的对象,它还需要使用相同的引用,例如objA === objB
)。您需要一个功能,该功能允许您提供可以检查条件的功能。幸运的是,有Array.prototype.some
个问题。
myarr.some(item => item.class === 'y')
答案 2 :(得分:1)
您可以使用Array#some
,也可以利用适当的数据结构。
const myArr = [{
type: "a",
class: "x",
value: "p"
},
{
type: "b",
class: "x",
value: "r"
},
{
type: "a",
class: "y",
value: "p"
}
]
const hasClassY = myArr.some (o => o.class == 'y')
console.log (hasClassY)
Map
而不是Array
const classMap = new Map([
['x', [{
type: "a",
class: "x",
value: "p",
}, {
type: "b",
class: "x",
value: "r"
}]],
['y', [{
type: "a",
class: "y",
value: "p"
}]]
])
if (classMap.has('y')) {
// Do stuff here
}
const itemsWithYClass = classMap.get('y')
const itemsWithXClass = classMap.get('x')
const flatten = xs => [...xs].reduce((r, el) => [...r, ...el])
const allItems = flatten(classMap.values())
console.log('Y class: ', itemsWithYClass)
console.log('X class: ', itemsWithXClass)
console.log('All: ', allItems)
Map
class SpecializedMap extends Map {
get[Symbol.species]() {
return Map
}
getAll() {
return [...this.values()].reduce((r, el) => [...r, ...el])
}
}
const classMap = new SpecializedMap([
['x', [{
type: "a",
class: "x",
value: "p",
}, {
type: "b",
class: "x",
value: "r"
}]],
['y', [{
type: "a",
class: "y",
value: "p"
}]]
])
if (classMap.has('y')) {
// Do stuff here
}
const itemsWithYClass = classMap.get('y')
const itemsWithXClass = classMap.get('x')
const allItems = classMap.getAll()
console.log('Y class: ', itemsWithYClass)
console.log('X class: ', itemsWithXClass)
console.log('All: ', allItems)
const myArr = [{
type: "a",
class: "x",
value: "p"
},
{
type: "b",
class: "x",
value: "r"
},
{
type: "a",
class: "y",
value: "p"
}
]
// This set contains all unique classes within myArr
// The challenge is you need to sync myArr with classSet
// so classSet contains the actual classes within myArr.
const classSet = new Set(['x', 'y'])
// Somewhere in your code you import classSet and...
if (classSet.has('y')) {
console.log('Do stuff here!')
}
答案 3 :(得分:1)
尝试这个简单的解决方案。
let myarr = [
{type:"a", class:"x", value:"p"},
{type:"b", class:"x", value:"r"},
{type:"a", class:"y", value:"p"}
];
console.log(myarr.some(function(element){return element["class"] === "x";}))
答案 4 :(得分:0)
您可以使用Array.prototype.includes()或String.prototype.includes(),但是不切实际,在这种情况下不能用作通用解决方案,请参见:
let myarr = [
{type:"a", class:"x", value:"p"},
{type:"b", class:"x", value:"r"},
{type:"a", class:"y", value:"p"}
];
//String.includes - We need the exact match, this could lead to error
var value1 = JSON.stringify(myarr).includes('"class":"x"');
//Array.includes - It's ok, but it has two iterations, not one as some()
var value2 = myarr.map(o=>o.class).includes("x")
console.log(value1)
console.log(value2)
这就是为什么使用其他用户所说的Some()
是更好的选择的原因。
答案 5 :(得分:-2)
我将lodash的.includes()或.has()方法用于诸如此类的事情:
_.includes({ 'a': 1, 'b': 2 }, 1);
https://lodash.com/docs/4.17.10#includes
您也可以使用普通的js映射,某些或其他方法来执行此操作。