0

Problem is, I make an empty array and it stays empty until a user is on my page and inputs their "username" and their "ID" and clicks a button.

I have to check the array for duplicates so I want my code to check the array, if nothing with the same name exists then add it into the array. Here is my code that does not work

var myArray = []; 

function arrayChecker(inputName, inputID) {
    for (var key in myArray) {
        if (myArray.hasOwnProperty(key)) {
            console.log(key, myArray[key]);
        } else {
            myArray.push({"name": inputName, "id": inputID});           
        }
    }  
}

This does not work however. Any ideas?

argabri
  • 1
  • 1
  • 1

4 Answers4

0

A better implementation:

function arrayChecker(inputName, inputID) {
    if (!myArray.some(x => x.name === inputName && x.id == inputID)) {
      myArray.push({"name": inputName, "id": inputID});
    }
}
ema
  • 5,668
  • 1
  • 25
  • 31
0

you do not check for duplicates. the myArray.hasOwnProperty(key) part only checks if the index (key) is in the array. however, you mostly want to check if the object on index=key has name==inputName and id==inputID. so your code needs to look something like that:

var myArray = []; 

function arrayChecker(inputName, inputID) {

    var check=false;

    for (var key in myArray) {
        if (myArray.hasOwnProperty(key)) {
            if (myArray[key].name == inputName && myArray[key].id == inputID)
                check=true;
        }  
    }

    if (!check)
        myArray.push({"name": inputName, "id": inputID});
}
wayneOS
  • 1,427
  • 1
  • 14
  • 20
0
var myArray = []; 

function arrayChecker(inputName, inputID) {
    if (!myArray.find(user=>user.name===inputName)){ // you can also check the id if necessarr
       myArray.push({"name": inputName, "id": inputID});          
    } else {
       console.log('user already exists')
    }

}
Miguel Giménez
  • 425
  • 4
  • 10
  • Hi Miguel thanks for the answer. I am getting an "Unexpected token >" on the line that has the if statement in my IDE program I am using. I am not sure if it is me or an error in the code? Any ideas? – argabri Feb 23 '18 at 14:20
  • you probably are running a version of node that doesnt accept arrow functions... – Miguel Giménez Feb 23 '18 at 15:54
0

What you need to use is a for-of-loop and check for the specific values:

Old fashion way:

function arrayChecker(inputName, inputID) {
    for (var obj of myArray) {
        if (obj.name === inputName && obj.id === inputID) {
            return false;
        }
    }  

    myArray.push({"name": inputName, "id": inputID});

    return true;
}

Using function some:

function arrayChecker(inputName, inputID) {
    var found = myArray.some(function (obj) {
        return obj.name === inputName && obj.id === inputID
    });

    if (found) return false;

    myArray.push({"name": inputName, "id": inputID});

    return true;
}

Important: The function arrayChecker returns the result for checking purposes, i.e: If the object was added or not. true was added and false wasn't added.

Ele
  • 33,468
  • 7
  • 37
  • 75
  • This it the best one by far, thank you. When I try the code it seems that if I try and compare with an object that was pushed to it by the myArray.push statement below it is not able to correctly compare and found always returns false. However, if I make the object in the code such as myArray.push({"name": "phillip, "id": "123"}) then the script will correctly compare the name and ID and identify if it existed already. This means it is something with my parameters/inputs? – argabri Feb 23 '18 at 14:40