I have this array:
[{"name": "Olvier", "id": 123 }, {"name": "Olvier", "id": 124 }]
Now my goal is to prevent adding the same object again to the array:
{"name": "Olvier", "id": 123 }
Is there a Method to do this?
I have this array:
[{"name": "Olvier", "id": 123 }, {"name": "Olvier", "id": 124 }]
Now my goal is to prevent adding the same object again to the array:
{"name": "Olvier", "id": 123 }
Is there a Method to do this?
If your object is created earlier and has the same reference as the one in the array, you can use indexOf:
var myObj = { a: 'b' };
myArray.push(myObj);
var isInArray = myArray.indexOf(myObj) !== -1;
Otherwise you can check it with find:
var isInArray = myArray.find(function(el){ return el.id === '123' }) !== undefined;
If the id's are unique you can simply check if an object with a certain id exists:
const collection = [
{"name": "Olvier", "id": 123 },
{"name": "Olvier", "id": 124 }
];
let person = { "name": "Olvier", "id": 123 };
function addPerson(person) {
const existingIds = collection.map((addedPerson) => addedPerson.id);
if (! existingIds.includes(person.id)) {
collection.push(person);
}
}
addPerson(person);
You could use Array#some method to determinate that item is already in array or not:
var arr = [
{ name: 'Olvier', id: 123 },
{ name: 'Olvier', id: 124 }
];
var existingItem = arr[1];
var newItem = { name: 'John', id: 125 };
if (!arr.some(item => item === existingItem)) {
arr.push(existingItem);
}
console.log(arr);
if (!arr.some(item => item === newItem)) {
arr.push(newItem);
}
console.log(arr);
Recreate the array with Array#reduce
and checking with Array#filter
var arr =[{"name": "Olvier", "id": 123 }, {"name": "Olvier", "id": 124 }]
var res = arr.reduce((a,b)=>{
if(a.filter(i=> i.name == b.name).length == 0){
a.push(b)
}
return a
},[])
console.log(res)
You can write custom method to insert new object into array. Before adding new element in the array, first check if the item exist in the array. You can use array#some
var arr = [{"name": "Olvier", "id": 123 }, {"name": "Olvier", "id": 124 }];
var obj1 = {"name": "Olvier", "id": 123 }, obj2 = {"name": "Olvier", "id": 125 };
var insertUniqueObject = function(arr, obj) {
let isExist = arr.some(o => o.name === obj.name && o.id === obj.id);
if(!isExist)
arr.push(obj);
return arr;
}
console.log(insertUniqueObject(arr, obj1));
console.log(insertUniqueObject(arr, obj2));
var myarray = [{"name": "Olvier", "id": 123 }, {"name": "Olvier", "id": 124 }];
var myobj =
{"name": "Olvier", "id": 123 };
var result = myarray.filter(function (item) {
return (item.name == myobj.name && item.id == myobj.id);
});
if(result.length==0){
//add here
}
Probably an id -> object Map is the better datastructure here. You could implement a wrapper for it:
class UserList extends Map {
constructor(arr){
super(arr.map(obj => [obj.id,obj]));
}
add(obj){
if(!this.has(obj.id)){
return this.set(obj.id,obj);
}
return false
}
remove(obj){
this.delete(obj.id || obj);
}
}
var users = new UserList([{"name": "Olvier", "id": 123 }, {"name": "Olvier", "id": 124 }]);
console.log(
users.add({name:"Jack",id:123}),//false
users.add({name:"Jack",id:125}),//works
users.delete(124),//removes Olvier
users.get(125),//Jack
[...users.entries()],//a 2d array of [id,obj]
[...users.values()]//objects only array
);
A very verbose way of doing it would be sth like:
var IsIncluded = false;
for(var i; i< array.length; i++) {
if(array[i].name == ObjectToInclude.name && array[i].id == ObjectToInclude.id ) {
IsIncluded = true;
break;
}
}
if(!IsIncluded) {
array.push(ObjectToInclude);
}