我有这个数组有对象和属性。 我需要知道字符串是否出现在此数组中任何行的属性“text”中。我知道这可以与.filter和.lenght一起使用,但我正在用lodash做这件事
const myArray=
[
{id: 6, text: "error1", isError: true}
{id: 7, text: "error2", isError: true}
]
示例:
lodash.contains(myArray,“error1”,“text”)看起来似乎不是正确的结构
答案 0 :(得分:0)
尝试
_.some(myArray, item => item.text === "error1")
答案 1 :(得分:0)
正如@CRice所说,您可以使用_.some
。实际上,您不需要lodash
,因为有可用的原始Array.prototype.some
方法:
const myArray = [
{id: 6, text: "error1", isError: true},
{id: 7, text: "error2", isError: true}
];
console.log(myArray.some(item => item.text === "error1"));