Javascript Array.push()和唯一项
我有一个将唯一值推入数组的简单例子。看起来是这样的:Javascript Array.push()和唯一项,javascript,Javascript,我有一个将唯一值推入数组的简单例子。看起来是这样的: this.items = []; add(item) { if(this.items.indexOf(item) > -1) { this.items.push(item); console.log(this.items); } } 看起来很直截了当,对吧?不,看起来是这样。它不会添加任何值。我肯定这是我犯的某种愚蠢的错误,但我似乎找不到它。如果它等于-1,则必须使用==-1,即项
this.items = [];
add(item) {
if(this.items.indexOf(item) > -1) {
this.items.push(item);
console.log(this.items);
}
}
看起来很直截了当,对吧?不,看起来是这样。它不会添加任何值。我肯定这是我犯的某种愚蠢的错误,但我似乎找不到它。如果它等于-1,则必须使用==-1,即项目在您的数组中不可用:
this.items = [];
add(item) {
if(this.items.indexOf(item) === -1) {
this.items.push(item);
console.log(this.items);
}
}
是的,这是个小错误
if(this.items.indexOf(item) === -1) {
this.items.push(item);
console.log(this.items);
}
你的逻辑是,“如果这个项目已经存在,那么就添加它。”它应该是相反的
把它改成
if (this.items.indexOf(item) == -1) {
this.items.push(item);
}
因此,不确定这是否回答了您的问题,但您正在添加的项目的索引继续返回-1。不太熟悉js,但似乎这些项是这样做的,因为它们还不在数组中。我为您编写了一些经过修改的代码
this.items = [];
add(1);
add(2);
add(3);
document.write("added items to array");
document.write("<br>");
function add(item) {
//document.write(this.items.indexOf(item));
if(this.items.indexOf(item) <= -1) {
this.items.push(item);
//document.write("Hello World!");
}
}
document.write("array is : " + this.items);
this.items=[];
增加(1);
增加(2);
增加(3);
document.write(“将项添加到数组”);
文件。写(“
”);
功能添加(项目){
//编写(此.items.indexOf(item));
如果(this.items.indexOf(item)我猜ES6已经设置了数据结构,您可以将其用于唯一的条目您可以使用ES6中的结构使代码更快、更可读:
// Create Set
this.items = new Set();
add(item) {
this.items.add(item);
// Set to array
console.log([...this.items]);
}
如果使用Lodash,请查看函数:
let items = [];
items = _.union([item], items)
var-helper={};
对于(变量i=0;i
试试看
大概是
const array = [1, 3];
if (!array.includes(2))
array.push(2);
但是,请注意页面底部的浏览器兼容性。在数组中始终推送唯一值
ab = [
{"id":"1","val":"value1"},
{"id":"2","val":"value2"},
{"id":"3","val":"value3"}
];
var clickId = [];
var list = JSON.parse(ab);
$.each(list, function(index, value){
if(clickId.indexOf(value.id) < 0){
clickId.push(value.id);
}
});
ab=[
{“id”:“1”,“val”:“value1”},
{“id”:“2”,“val”:“value2”},
{“id”:“3”,“val”:“value3”}
];
var clickId=[];
var list=JSON.parse(ab);
$.each(列表、函数(索引、值){
如果(clickId.indexOf(value.id)<0){
点击id.push(value.id);
}
});
使用
如果你要找一艘班轮
原语
(this.items.indexOf(item) === -1) && this.items.push(item);
对于对象
this.items.findIndex((item: ItemType) => item.var === checkValue) === -1 && this.items.push(item);
Set()
是的,但我需要一个数组。集合也是一个数组,存储顺序数据,但只有唯一的一个不重复。@Gags不正确,请在所有主要的最新浏览器上工作。作为提醒。如果你像我一样在代码中使用JSON.stringify,添加集合将导致问题,无需进一步更新。我喜欢这个。我几乎忘记了集合。我将使用t他写了很多,尤其是打字脚本。@JohnDuskin你能详细说明一下吗?你的评论可能会暗示在“你的代码”中使用JSON.stringify,就像在一个作用域?中一样,也使用Set对象,就像在这样的作用域/代码中的其他地方一样?或者更可能的是,你是指使用Set对象作为JSON.stringify的参数。就像在一个表达式中使用它们一样。有了解决方案。@papo你是对的,在重新阅读我的注释时,它是模糊的。你的解释是正确的,将集合添加到JSON中.stringify命令导致问题(根据您链接的问题)解决方案是需要将其转换为数组。但是,如果要对包含集合的更高级别对象进行字符串化,这可能会更复杂,因此这是人们应该注意的。我认为这肯定比.indexOf
更优雅。我以前从来都不知道这一点。@jagsSparrow您能帮助解释一下indexOf
help to find unique Alo?@AnshulRiyal您可以查看您是否缺少表达式的括号“(”)
this.items = new Set();
this.items.add(1);
this.items.add(2);
this.items.add(1);
this.items.add(2);
console.log(Array.from(this.items)); // [1, 2]
(this.items.indexOf(item) === -1) && this.items.push(item);
this.items.findIndex((item: ItemType) => item.var === checkValue) === -1 && this.items.push(item);