push to JSON Array if Object does not exist

push to JSON Array if Object does not exist



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)

);





your logic is fair enough. only thing is you need to update idsarray with new element added
– Bhushan Kawadkar
Aug 20 at 11:03


idsarray




3 Answers
3



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.


jQuery.inArray(newid, idsarray) === -1


jQuery.inArray(newid, idsarray)


-1


newid


idsarray


idsarray



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


indexOf()


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()


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>



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:


inArray


find


some


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:


Map


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



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


Map


test.values()



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.


array


id




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).


ids


map


set




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);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj