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.

S.Negi
  • 51
  • 7
  • What for I got -1 ? – S.Negi Feb 07 '18 at 19:59
  • I did not downvote you but it's probably for lack of own research. There already is a similar question here: https://stackoverflow.com/questions/22844560/check-if-object-value-exists-within-a-javascript-array-of-objects-and-if-not-add and while it uses `Array.prototype.some` that is part of ES6 (hence not usable for you) you can always implement polyfill. – Ivan Sivak Feb 07 '18 at 20:14
  • ..also, the same for `unshift` alternatives (e.g. `[newObj].concat(array)`). Discussed here: https://stackoverflow.com/questions/6195729/most-efficient-way-to-prepend-a-value-to-an-array the unshift should be the fastest. – Ivan Sivak Feb 07 '18 at 20:17
  • @IvanSivak are you saying it can't be done using ES5? – S.Negi Feb 07 '18 at 20:18
  • where did I say "it can't be done in ES5"? :) – Ivan Sivak Feb 07 '18 at 20:21
  • @IvanSivak Hey, MDN says some() method is available since ES5.1, but you mentioned ES6. No, I didn't mean that way ;p – S.Negi Feb 07 '18 at 20:26
  • Sure, but the most important part of my comment was the word `polyfill` :) If you check that MDN documentation again and scroll to the section titled "Polyfill" you will understand ;) – Ivan Sivak Feb 07 '18 at 20:28
  • 1
    I found the solution, mentioned by @IvanSivak in comments. – S.Negi Feb 07 '18 at 20:42
  • I would [have gone about it like this](https://gist.github.com/Tiny-Giant/b4479949712bf68610a51d00e82678b8) for your situation. –  Feb 07 '18 at 22:14
  • @TinyGiant, I only had to do that operation once. That method is beneficial if you're doing the checking at multiple places. That was a great solution though. Thanks for sharing. – S.Negi Feb 08 '18 at 04:33

2 Answers2

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

Community
  • 1
  • 1
Pavlo
  • 1,157
  • 7
  • 13
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;

}
RyanDay
  • 1,856
  • 16
  • 23