Easy way to prevent add object with the same value to array [duplicate]
This question already has an answer here:
Array.push() if does not exist?
17 answers
Like in title easy way to prevent add object with the same value to array
const array = [
{
name:'John'
},
{
name: 'Mark'
}
]
array.push({name: 'John'}) //don't add
array.push({name: 'Kevin'}) //add
console.log(array)
javascript
marked as duplicate by Emissary, adiga, mplungjan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 9:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Array.push() if does not exist?
17 answers
Like in title easy way to prevent add object with the same value to array
const array = [
{
name:'John'
},
{
name: 'Mark'
}
]
array.push({name: 'John'}) //don't add
array.push({name: 'Kevin'}) //add
console.log(array)
javascript
marked as duplicate by Emissary, adiga, mplungjan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 9:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
please post here what you have tried so far
– Pierre
Nov 19 '18 at 8:29
2
You could check if an item exist using someif(!array.some(item=>item.name===toAdd.name)) array.push(toAdd)
– HMR
Nov 19 '18 at 8:30
Great thanks for help
– tobi1512
Nov 19 '18 at 8:36
add a comment |
This question already has an answer here:
Array.push() if does not exist?
17 answers
Like in title easy way to prevent add object with the same value to array
const array = [
{
name:'John'
},
{
name: 'Mark'
}
]
array.push({name: 'John'}) //don't add
array.push({name: 'Kevin'}) //add
console.log(array)
javascript
This question already has an answer here:
Array.push() if does not exist?
17 answers
Like in title easy way to prevent add object with the same value to array
const array = [
{
name:'John'
},
{
name: 'Mark'
}
]
array.push({name: 'John'}) //don't add
array.push({name: 'Kevin'}) //add
console.log(array)
This question already has an answer here:
Array.push() if does not exist?
17 answers
javascript
javascript
asked Nov 19 '18 at 8:28
tobi1512tobi1512
247
247
marked as duplicate by Emissary, adiga, mplungjan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 9:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Emissary, adiga, mplungjan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 9:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
please post here what you have tried so far
– Pierre
Nov 19 '18 at 8:29
2
You could check if an item exist using someif(!array.some(item=>item.name===toAdd.name)) array.push(toAdd)
– HMR
Nov 19 '18 at 8:30
Great thanks for help
– tobi1512
Nov 19 '18 at 8:36
add a comment |
please post here what you have tried so far
– Pierre
Nov 19 '18 at 8:29
2
You could check if an item exist using someif(!array.some(item=>item.name===toAdd.name)) array.push(toAdd)
– HMR
Nov 19 '18 at 8:30
Great thanks for help
– tobi1512
Nov 19 '18 at 8:36
please post here what you have tried so far
– Pierre
Nov 19 '18 at 8:29
please post here what you have tried so far
– Pierre
Nov 19 '18 at 8:29
2
2
You could check if an item exist using some
if(!array.some(item=>item.name===toAdd.name)) array.push(toAdd)
– HMR
Nov 19 '18 at 8:30
You could check if an item exist using some
if(!array.some(item=>item.name===toAdd.name)) array.push(toAdd)
– HMR
Nov 19 '18 at 8:30
Great thanks for help
– tobi1512
Nov 19 '18 at 8:36
Great thanks for help
– tobi1512
Nov 19 '18 at 8:36
add a comment |
1 Answer
1
active
oldest
votes
The prototype
constructor allows you to add new properties and methods to the Array()
object.
So, you can create a method addUser
and validate that the new user is unique:
const array = [{name: 'John'},{name: 'Mark'}];
Array.prototype.addUser = function(user) {
if (!this.some(u => u.name === user.name)) {
this.push(user);
}
return this;
};
array.addUser({name: 'John'}); //don't add
array.addUser({name: 'Kevin'}); //add
console.log(array);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The prototype
constructor allows you to add new properties and methods to the Array()
object.
So, you can create a method addUser
and validate that the new user is unique:
const array = [{name: 'John'},{name: 'Mark'}];
Array.prototype.addUser = function(user) {
if (!this.some(u => u.name === user.name)) {
this.push(user);
}
return this;
};
array.addUser({name: 'John'}); //don't add
array.addUser({name: 'Kevin'}); //add
console.log(array);
add a comment |
The prototype
constructor allows you to add new properties and methods to the Array()
object.
So, you can create a method addUser
and validate that the new user is unique:
const array = [{name: 'John'},{name: 'Mark'}];
Array.prototype.addUser = function(user) {
if (!this.some(u => u.name === user.name)) {
this.push(user);
}
return this;
};
array.addUser({name: 'John'}); //don't add
array.addUser({name: 'Kevin'}); //add
console.log(array);
add a comment |
The prototype
constructor allows you to add new properties and methods to the Array()
object.
So, you can create a method addUser
and validate that the new user is unique:
const array = [{name: 'John'},{name: 'Mark'}];
Array.prototype.addUser = function(user) {
if (!this.some(u => u.name === user.name)) {
this.push(user);
}
return this;
};
array.addUser({name: 'John'}); //don't add
array.addUser({name: 'Kevin'}); //add
console.log(array);
The prototype
constructor allows you to add new properties and methods to the Array()
object.
So, you can create a method addUser
and validate that the new user is unique:
const array = [{name: 'John'},{name: 'Mark'}];
Array.prototype.addUser = function(user) {
if (!this.some(u => u.name === user.name)) {
this.push(user);
}
return this;
};
array.addUser({name: 'John'}); //don't add
array.addUser({name: 'Kevin'}); //add
console.log(array);
const array = [{name: 'John'},{name: 'Mark'}];
Array.prototype.addUser = function(user) {
if (!this.some(u => u.name === user.name)) {
this.push(user);
}
return this;
};
array.addUser({name: 'John'}); //don't add
array.addUser({name: 'Kevin'}); //add
console.log(array);
const array = [{name: 'John'},{name: 'Mark'}];
Array.prototype.addUser = function(user) {
if (!this.some(u => u.name === user.name)) {
this.push(user);
}
return this;
};
array.addUser({name: 'John'}); //don't add
array.addUser({name: 'Kevin'}); //add
console.log(array);
edited Nov 19 '18 at 9:01
answered Nov 19 '18 at 8:48
Yosvel QuinteroYosvel Quintero
11.2k42430
11.2k42430
add a comment |
add a comment |
please post here what you have tried so far
– Pierre
Nov 19 '18 at 8:29
2
You could check if an item exist using some
if(!array.some(item=>item.name===toAdd.name)) array.push(toAdd)
– HMR
Nov 19 '18 at 8:30
Great thanks for help
– tobi1512
Nov 19 '18 at 8:36