1

I have an array of objects like

[
  {
    "name": "Name 1",
    "id": "1245"
  },
  {
    "name": "Name 2",
    "id": "9788"
  },
  {
    "name": "Name 3",
    "id": "5694"
  },
  {
    "name": "Name 4",
    "id": "4523"
  },
  {
    "name": "Name 5",
    "id": "4567"
  }
]

I need to check if the array contains an object with an ID (let's say "1111"). If ID not found in any of the objects inside array, then place the new object(e.g., {"name":"Test 0","id":"1111"}) at the beginning of the array.

PS: Is there methods other than array.unshift(newObj), to achieve the goal?

Edit: Okay. I did tried using array.indexOf(), but it seems like it only works with values not objects. I also try looping through the array, but didn't worked too. I cannot use ES6.

10
  • What for I got -1 ? Commented Feb 7, 2018 at 19:59
  • I did not downvote you but it's probably for lack of own research. There already is a similar question here: stackoverflow.com/questions/22844560/… and while it uses Array.prototype.some that is part of ES6 (hence not usable for you) you can always implement polyfill. Commented Feb 7, 2018 at 20:14
  • ..also, the same for unshift alternatives (e.g. [newObj].concat(array)). Discussed here: stackoverflow.com/questions/6195729/… the unshift should be the fastest. Commented Feb 7, 2018 at 20:17
  • @IvanSivak are you saying it can't be done using ES5? Commented Feb 7, 2018 at 20:18
  • 1
    I found the solution, mentioned by @IvanSivak in comments. Commented Feb 7, 2018 at 20:42

2 Answers 2

0

Without ES6 and assuming your object is named users you can conditionally add one if it doesn't exist like this:

function addUser(name, id) {

    users.forEach(function(user) {
        if (user.id === id) {
            return user;
        }
    });

    var newUser = {"name": name,"id": id}

    users.unshift(newUser);

    return newUser;

}
Sign up to request clarification or add additional context in comments.

2 Comments

Close, the original poster wants to add the new data at the beginning of the array, not at the end.
Ah, I missed that. Edited
0

Using reduce

function hasId(array, id){
    return array.reduce(function(sum, element){
        return sum || element.id === id;
    }, false);
}

if(hasId(array, 1111)){
    array.splice(0, 0, newElement);  
}

There are 1000 ways to do this. This solution works with es5, it isn't too efficient because it runs through the entire array. The fastest way is to do it in a for-loop, then return on the first found, but this looks prettier.

Using for loop

function hasId(array, id){
   for(var i=0; i<array.length;i++){
       if(array[i].id === id){
           return true;
       }
   }
   return false;
}

if(hasId(array, 1111)){
    array.splice(0, 0, newElement);  
}

This is the fastest method, it stops as soon as the first id is found

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.