gpt4 book ai didi

javascript - 如何检查 vue.js 数组中是否已存在项目?

转载 作者:行者123 更新时间:2023-11-28 18:11:45 25 4
gpt4 key购买 nike

我有一个数组,我在单击时添加值,但我想检查值是否已经在数组中,如果它不执行任何操作。我尝试使用indexOf,但每次都得到相同的结果

this.fields.push(this.field);
this.field = { value: '' };

最佳答案

在对象数组中,最好使用Array.some()

this.users = [{id : 1, name : 'Jonh Doe'},{id : 2, name : 'Mary Jean'}]

if(!this.users.some(data => data.id === 1)){
//don't exists
}else{
//exists because Jonh Doe has id 1
}

来源:https://www.samanthaming.com/

关于javascript - 如何检查 vue.js 数组中是否已存在项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454247/

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