const students = [
{id: 1, name: 'Cal', location: 'McHale' },
{id: 2, name: 'Courtney', location: 'Sydney Hall' },
{id: 1, name: 'Cal', location: 'Syndey hall' }
]
因此,我的预期输出将抓住所有ID的实例:1.
{id: 1, name: 'Cal', location: 'McHale' },
{id: 1, name: 'Cal', location: 'Syndey hall' }
我最终想删除重复的名称并在列表中显示……(但这是在线的。现在,我只想抓住匹配的对象)。
Id: 1 Name: Cal Location: McHale
Syndey Hall
我尝试了:
const result = _.find(students, {student_id: studentId});
但这似乎不起作用,它只是返回具有该ID的对象之一。
{id: 1, name: 'Cal', location: 'McHale' },
我可以做这项工作?
I将调查
滤波器
函数。它已构建为JavaScript。这里是其工作原理的一个例子。您需要做的就是找到一种方法来确定其是否具有适当ID的函数。
function isBigEnough(value) {
return value >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
一样
_.filter
const result = _.filter(students, {student_id: studentId});
toind cind将始终返回第一个匹配元素,无论其他元素可能与您的病情匹配多少。 如果您想提取所有匹配元素,则需要使用.filter方法。
实现是相同的,但是结果从对象(.find(.find())到数组(.filter())