0

I have a JSON as shown below

[
  {
    "name": "Mike",
    "incentives": "23.45",
    "id": "1"
  },
  {
    "name": "Larsen",
    "incentives": "34.78",
    "id": "2"
  },
  {
    "name": "Steve",
    "incentives": "26.78",
    "id": "3"
  }
]

I need to push a new JSON Object into the JSON array if id not exists inside the JSON Object

tried as this way and working , any better way of doing this

$(document).ready(function() {

var idsarray = [];

  var test = [
  {
    "name": "Mike",
    "incentives": "23.45",
    "id": "1"
  },
  {
    "name": "Larsen",
    "incentives": "34.78",
    "id": "2"
  },
  {
    "name": "Steve",
    "incentives": "26.78",
    "id": "3"
  }
];

for(var i=0;i<test.length;i++)
{
idsarray.push(test[i].id)
}

var newobj =  {
    "name": "RAM",
    "incentives": "56.78",
    "id": "4"
  }

  var newid = newobj.id;

if(jQuery.inArray(newid, idsarray) !== -1)
{
test.push(newobj)
}

alert(test.length)

});
1
  • your logic is fair enough. only thing is you need to update idsarray with new element added Commented Aug 20, 2018 at 11:03

3 Answers 3

1

Your condition is incorrect. It should be jQuery.inArray(newid, idsarray) === -1 because the operation jQuery.inArray(newid, idsarray) will return -1 if the newid is not present in idsarray. So, that is what you need, to check unique occurrence in idsarray.

You can also use the indexOf() operation for the same. idsarray.indexOf(newid) === -1.

OP's code

$(document).ready(function() {

  var idsarray = [];
  var test = [{
      "name": "Mike",
      "incentives": "23.45",
      "id": "1"
    },
    {
      "name": "Larsen",
      "incentives": "34.78",
      "id": "2"
    },
    {
      "name": "Steve",
      "incentives": "26.78",
      "id": "3"
    }
  ];

  for (var i = 0; i < test.length; i++) {
    idsarray.push(test[i].id)
  }

  var newobj = {
    "name": "RAM",
    "incentives": "56.78",
    "id": "4"
  }

  var newid = newobj.id;
  if (jQuery.inArray(newid, idsarray) === -1) {
    test.push(newobj)
  }
  alert(test.length)

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

USING indexOf()

$(document).ready(function() {

  var idsarray = [];
  var test = [{
      "name": "Mike",
      "incentives": "23.45",
      "id": "1"
    },
    {
      "name": "Larsen",
      "incentives": "34.78",
      "id": "2"
    },
    {
      "name": "Steve",
      "incentives": "26.78",
      "id": "3"
    }
  ];

  for (var i = 0; i < test.length; i++) {
    idsarray.push(test[i].id)
  }

  var newobj = {
    "name": "RAM",
    "incentives": "56.78",
    "id": "4"
  }

  var newid = newobj.id;
  if (idsarray.indexOf(newobj) === -1) {
    test.push(newobj)
  }
  alert(test.length)

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

1

inArray is slow. If you do this once, it's not the worst thing ever, but find or some on the original array would be better:

if (!test.some(o => o.id == newid)) {
  test.push(newobj)
}

However, if you do this often, a much better idea is to make a set, as checking membership in a set is very very fast:

let idset = new Set();
...
if (!idset.has(newid)) {
  test.push(newobj);
  idset.add(newid);
}

You could also use Map (or even plain old object) to combine the two:

let test = new Map();
...
if (!test.has(newid)) {
    test.set(newobj);
}

You can get the array from the Map using test.values().

Comments

0

You can avoid the use of another array that contains the id of objects. You can use native function Array.some to check for existence and push accordingly.

var test = [{"name":"Mike","incentives":"23.45","id":"1"},{"name":"Larsen","incentives":"34.78","id":"2"},{"name":"Steve","incentives":"26.78","id":"3"}];
var newobj = {"name":"RAM","incentives":"56.78","id":"4"};
if(!test.some(({id}) => id == newobj.id)) test.push(newobj);
console.log(test);

Also, if your push operation is going to be quite frequent then looping every time will affect the performance. Instead of array of ids, you can maintain an map or set of ids as the complexity of search will be O(1).

var test = [{"name":"Mike","incentives":"23.45","id":"1"},{"name":"Larsen","incentives":"34.78","id":"2"},{"name":"Steve","incentives":"26.78","id":"3"}];
var newobj = {"name":"RAM","incentives":"56.78","id":"4"};
var ids = test.reduce((a, {id}) => Object.assign(a, {[id]:id}), {});
if(!ids[newobj.id]) {
   test.push(newobj);
   ids[newobj.id] = newobj.id;
}
console.log(test);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.