Easy way to prevent add object with the same value to array [duplicate]












-2
















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)









share|improve this question













marked as duplicate by Emissary, adiga, mplungjan javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 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
















-2
















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)









share|improve this question













marked as duplicate by Emissary, adiga, mplungjan javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 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














-2












-2








-2


1







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)









share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 8:28









tobi1512tobi1512

247




247




marked as duplicate by Emissary, adiga, mplungjan javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















0














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);








share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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);








    share|improve this answer






























      0














      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);








      share|improve this answer




























        0












        0








        0







        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);








        share|improve this answer















        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);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 9:01

























        answered Nov 19 '18 at 8:48









        Yosvel QuinteroYosvel Quintero

        11.2k42430




        11.2k42430

















            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()