gpt4 book ai didi

javascript - 在推送项目之前检查属性值是否存在

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:18 24 4
gpt4 key购买 nike

下面是我的示例代码,它通过检查项目是否不存在来推送到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com