如果两个值都不存在,我如何推入数组?这是我的数组:
[
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
如果我试图再次推入数组的名字:“tom”或文本:“tasty”,我不希望发生任何事情…但如果这两个都不存在那么我就输入。push()
我该怎么做呢?
http://api.jquery.com/jQuery.unique/
var cleanArray = $.unique(clutteredArray);
你可能对makeArray也感兴趣
前面的例子最好说明在push之前检查它是否存在。
事后看来,它还声明你可以将它声明为原型的一部分(我猜这是又名类扩展),所以下面没有大的增强。
除了我不确定indexOf是一个更快的路径,然后inArray?可能。
Array.prototype.pushUnique = function (item){
if(this.indexOf(item) == -1) {
//if(jQuery.inArray(item, this) == -1) {
this.push(item);
return true;
}
return false;
}
正是出于这些原因,使用像underscore.js这样的js库。union:计算传入数组的并集:在一个或多个数组中出现的唯一项的列表。
_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
=> [1, 2, 3, 101, 10]
使用数组是很容易做到的。函数findIndex,它以函数作为参数:
var arrayObj = [{name:"bull", text: "sour"},
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
var index = arrayObj.findIndex(x => x.name=="bob");
// here you can check specific property for an object whether it exist in your array or not
index === -1 ? arrayObj.push({your_object}) : console.log("object already exists")
您可以使用foreach检查数组,然后弹出项目,如果它存在,否则添加新的项目…
newItemValue &submitFields是键值对
> //submitFields existing array
> angular.forEach(submitFields, function(item) {
> index++; //newItemValue new key,value to check
> if (newItemValue == item.value) {
> submitFields.splice(index-1,1);
>
> } });
submitFields.push({"field":field,"value":value});
可以使用带有回调函数及其"this"参数的findIndex方法。
注意:旧的浏览器不知道findIndex,但是一个polyfill是可用的。
示例代码(注意,在原始问题中,只有当一个新对象的数据都不在之前的推送对象中时,它才会被推送):
var a=[{name:"tom", text:"tasty"}], b;
var magic=function(e) {
return ((e.name == this.name) || (e.text == this.text));
};
b={name:"tom", text:"tasty"};
if (a.findIndex(magic,b) == -1)
a.push(b); // nothing done
b={name:"tom", text:"ugly"};
if (a.findIndex(magic,b) == -1)
a.push(b); // nothing done
b={name:"bob", text:"tasty"};
if (a.findIndex(magic,b) == -1)
a.push(b); // nothing done
b={name:"bob", text:"ugly"};
if (a.findIndex(magic,b) == -1)
a.push(b); // b is pushed into a
我知道这是一个非常老的问题,但如果你使用ES6,你可以使用一个非常小的版本:
[1,2,3].filter(f => f !== 3).concat([3])
非常简单,首先添加一个过滤器,删除项目-如果它已经存在,然后通过concat添加它。
下面是一个更现实的例子:
const myArray = ['hello', 'world']
const newArrayItem
myArray.filter(f => f !== newArrayItem).concat([newArrayItem])
如果你的数组包含对象,你可以像这样调整过滤器函数:
someArray.filter(f => f.some(s => s.id === myId)).concat([{ id: myId }])
不确定速度,但stringification + indexOf是一个简单的方法。首先将数组转换为字符串:
let strMyArray = JSON.stringify(myArray);
然后,对于一系列属性-值对,您可以使用:
if (strMyArray.indexOf('"name":"tom"') === -1 && strMyArray.indexOf('"text":"tasty"') === -1) {
myArray.push({ name: "tom", text: "tasty" });
}
查找整个对象更简单:
if (strMyArray.indexOf(JSON.stringify(objAddMe) === -1) {
myArray.push(objAddMe);
}
推动动态
var a = [
{name:"bull", text: "sour"},
{name: "tom", text: "tasty" },
{name: "Jerry", text: "tasty" }
]
function addItem(item) {
var index = a.findIndex(x => x.name == item.name)
if (index === -1) {
a.push(item);
}else {
console.log("object already exists")
}
}
var item = {name:"bull", text: "sour"};
addItem(item);
用简单的方法
var item = {name:"bull", text: "sour"};
a.findIndex(x => x.name == item.name) == -1 ? a.push(item) : console.log("object already exists")
如果数组只包含基元类型/简单数组
var b = [1, 7, 8, 4, 3];
var newItem = 6;
b.indexOf(newItem) === -1 && b.push(newItem);
这里你有一种方法可以在一行中为两个数组做这件事:
const startArray = [1,2,3,4]
const newArray = [4,5,6]
const result = [...startArray, ...newArray.filter(a => !startArray.includes(a))]
console.log(result);
//Result: [1,2,3,4,5,6]
推送后删除重复项
如果你已经有一个包含重复项的数组,将对象数组转换为字符串数组,然后使用Set()函数消除重复项:
let arr_obj = [
{ name: "tom", text: "tasty" },
{ name: "tom", text: "tasty" }
]
let arr_str = arr_obj.map(JSON.stringify)
let arr_unique = [...new Set(arr_str)].map(JSON.parse)
推前检查
如果你到目前为止没有重复的元素,你想在推入一个新元素之前检查重复:
let arr_obj = [
{ name: "tom", text: "tasty" },
{ name: "tim", text: "tusty" }
]
let new_obj = { name: "tom", text: "tasty" }
let arr_str = arr_obj.map(JSON.stringify)
!arr_str.includes(JSON.stringify(new_obj)) && arr_obj.push(new_obj)
这个问题有点老了,但我的选择是
let finalTab = [{id: 1, name: 'dupont'}, {id: 2, name: 'tintin'}, {id: 3, name:'toto'}]; // Your array of object you want to populate with distinct data
const tabToCompare = [{id: 1, name: 'dupont'}, {id: 4, name: 'tata'}]; // A array with 1 new data and 1 is contain into finalTab
finalTab.push(
...tabToCompare.filter(
tabToC => !finalTab.find(
finalT => finalT.id === tabToC.id)
)
); // Just filter the first array, and check if data into tabToCompare is not into finalTab, finally push the result of the filters
console.log(finalTab); // Output : [{id: 1, name: 'dupont'}, {id: 2, name: 'tintin'}, {id: 3, name: 'toto'}, {id: 4, name: 'tata'}];
我有这个问题,我做了一个简单的原型,使用它,如果你喜欢它
Array.prototype.findOrPush = function(predicate, fallbackVal) {
let item = this.find(predicate)
if(!item){
item = fallbackVal
this.push(item)
}
return item
}
let arr = [{id: 1}]
let item = arr.findOrPush(e => e.id == 2, {id: 2})
console.log(item) // {id: 2}
// will not push and just return existing value
arr.findOrPush(e => e.id == 2, {id: 2})
conslog.log(arr) // [{id: 1}, {id: 2}]
如果你的项目包含lodash,使用unionBy方法会很简单
import {unionBy} from "lodash";
let arrayObj = [
{ name: "jhon", text: "guitar"},
{ name: "paul", text: "bass" },
{ name: "george", text: "guitar" }
];
// this object will be added to the array
arrayObj = unionBy(arrayObj, [{name: 'ringo', text: 'drums'}], 'name')
// this object will be ignored because already exists
arrayObj = unionBy(arrayObj, [{name: "jhon", text: "guitar"}], 'name')
如果不在列表中,则添加
对于一个简单值的列表,它是一行程序…
[...new Set([...someArray, someElement])]
JavaScript的用法:
var myArray = ['bill','bob']
var alreadyIn = [...new Set([...myArray, 'bob'])] // ['bill','bob']
var notAlreadyIn = [...new Set([...myArray, 'peter'])] // ['bill','bob','peter']
TypeScript文本(注意include vs includes):
interface Array<T> {
include(element: T): Array<T>
}
Array.prototype.include = function (element: any): any[] {
return [...new Set([...this, obj])]
}
...但对于对象来说,情况就复杂多了
[...new Set([...someArray.map((o) => JSON.stringify(o)),
JSON.stringify(someElement)]).map((o) => JSON.parse(o))
TypeScript文本处理任何事情:
Array.prototype.include = function (element: any): any[] {
if (element && typeof element === 'object')
return [
...new Set([
...this.map((o) => JSON.stringify(o)),
JSON.stringify(element),
]),
].map((o) => JSON.parse(o))
else return [...new Set([...this, element])]
}