下面是我的示例代码,它通过检查项目是否不存在来推送到 VacanciesWithSavedSearches 数组。
if ($scope.VacanciesWithSavedSearches.indexOf(value) == -1) {
$scope.VacanciesWithSavedSearches.push({
type: "Saved Searches",
title: value.title,
value: value.id
});
}
如何通过将 indexOf 替换为实际属性值来更改上述内容,例如,如果列表不包含其他项目,则将项目添加到列表 VacanciesWithSavedSearches.id=123
最佳答案
使用array.filter
var result = $scope.VacanciesWithSavedSearches.filter(t=t.id ==='123');
if(result.length === 0)
{
$scope.VacanciesWithSavedSearches.push({
type: "Saved Searches",
title: value.title,
value: value.id
});
}
关于javascript - 在推送项目之前检查属性值是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241931/