如果我有一个像这样的对象数组:
const array = [
{name: "Jim", attributes: "strong, handsome, tall", age: 28},
{name: "Alice", attributes: "blonde, thin, tall", age: 26},
{name: "Bob", attributes: "lazy, small, thin", age: 32}
]是否有一种方法可以使用_.filter(array)创建一个带有对象的新数组,其中属性包含一个值。类似于_.filter(数组,attributes.contains("tall"))将返回所需的结果:
[
{name: "Jim", attributes: "strong, handsome, tall", age: 28},
{name: "Alice", attributes: "blonde, thin, tall", age: 26}
]发布于 2022-11-03 19:00:34
发布于 2022-11-03 19:01:41
这可以通过内置的filter和检查person的属性是否包括"tall"来完成。
const array = [
{name: "Jim", attributes: "strong, handsome, tall", age: 28},
{name: "Alice", attributes: "blonde, thin, tall", age: 26},
{name: "Bob", attributes: "lazy, small, thin", age: 32}
];
const tallPeople = array.filter(
(person) => person.attributes.includes("tall")
);
console.log(tallPeople);
https://stackoverflow.com/questions/74308229
复制相似问题