b.push(a);
如果b为空(b = [])或者值如下,如何避免添加重复项?
var a = ["username1", "1"]; var b = [ ["username1", "1"], ["username2", "2"] ]; b.push(a);
Nenad Vracar.. 5
您可以使用some()
并every()
首先检查是否a
存在b
,然后使用push.
var a = ["username1", "1"];
var b = [ ["username1", "1"], ["username2", "2"] ];
var check = b.some(function(e) {
return a.length == e.length && a.every(function(c) {
return e.includes(c)
})
})
if (!check) b.push(a);
console.log(b)
您可以使用some()
并every()
首先检查是否a
存在b
,然后使用push.
var a = ["username1", "1"];
var b = [ ["username1", "1"], ["username2", "2"] ];
var check = b.some(function(e) {
return a.length == e.length && a.every(function(c) {
return e.includes(c)
})
})
if (!check) b.push(a);
console.log(b)