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.