-2

I need to push a new ID for my data array. If I try pushing into data it creates one more object but not adding into the array for each.

Data:

[{"devices":{"dID":"TLSM01"},"uuid":"e863c776-f939-4761-bbce-bf0501b42ef7"},
 {"devices":{"dID":"TLSM01"},"uuid":"5a0cd70d-891d-48d8-b205-e92e828ac445"}]

Data need to be added:

{"EntityID":"12458412548"}

Final Result:

[{"devices":{"dID":"TLSM01","EntityID":"12458412548"},"uuid":"e863c776-f939-4761-bbce-bf0501b42ef7"},
 {"devices":{"dID":"TLSM01","EntityID":"12458412548"},"uuid":"5a0cd70d-891d-48d8-b205-e92e828ac445"}]

Code:

var data = [{
  "devices": {
    "dID": "TLSM01"
  },
  "uuid": "e863c776-f939-4761-bbce-bf0501b42ef7"
}, {
  "devices": {
    "dID": "TLSM01"
  },
  "uuid": "5a0cd70d-891d-48d8-b205-e92e828ac445"
}]
data.push({
  "EntityID": "test"
});
console.log(data);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    JSON is a *textual notation* for data exchange. [(More.)](http://stackoverflow.com/a/2904181/157247) By the time you're pushing a value into that array, it's not JSON. It's just an array. It may never have been JSON. – T.J. Crowder May 02 '17 at 07:10
  • Which language are you using? Javascript maybe? – Mario May 02 '17 at 07:10
  • *"If I try pushing into data it creates one more object but not adding into the array for each."* Show us your code for that. We can't help you with code we cannot see. – T.J. Crowder May 02 '17 at 07:11
  • I am using JavaScript – Karthikeyan Prakash May 02 '17 at 07:13
  • Can you simplify the example? An endlessly scrolling one-liner is impossible to fully comprehend. – deceze May 02 '17 at 07:16
  • If your "final result" is the *desired* result, then `data[0].devices.EntityID = "test"`, because you're trying to add a property to the object that is the first element in the array, not add an element to an array. Or use a loop to add the property to each element. – nnnnnn May 02 '17 at 07:16
  • @nnnnnn I have the suspicion he wants to add to *every* element in the array, but I'm not going to try to pick that gobbledygook apart… – deceze May 02 '17 at 07:19
  • @deceze - Yeah, I was just editing my comment to suggest a loop. It *is* very difficult to read with all the nesting all on one line like that. – nnnnnn May 02 '17 at 07:20

3 Answers3

1

data is an array containing objects. If you want to add a property to each object you have to iterate over the array.

You need to add a new property to the object devices which is not an array thus you cannot use .push()

var data = [{"devices":{"dID":"TLSM01"},"uuid":"e863c776-f939-4761-bbce-bf0501b42ef7"},{"devices":{"dID":"TLSM01"},"uuid":"5a0cd70d-891d-48d8-b205-e92e828ac445"}];

data.forEach(d=>d.devices['EntityID']="test");

console.log(data);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
  • Why use the brackets form? (Just curious.) Also note that their "final result" doesn't cap the `E` in `EntityID` although they did in their example. – T.J. Crowder May 02 '17 at 07:22
  • @T.J.Crowder I do not have any reason to use the brackets form. It was just in case he needs to use different names with symbols. Actually, OP is using an upper case `E` – Weedoze May 02 '17 at 07:24
  • Thanks -- they edited again! Previously they weren't. :-) – T.J. Crowder May 02 '17 at 07:26
0

If your "final result" is the result you want to achieve, you don't want to push anything. You're just setting a new property on the entries that already exist in the array. So, loop through and do that:

data.forEach(function(entry) {
    entry.EntityID = "12458412548";
});

(Or a simple for loop.)

If you're using ES2015+ syntax, you could use an arrow function:

data.forEach(entry => entry.EntityID = "12458412548");

...or a for-of loop:

for (const entry of data) {
    entry.EntityID = "12458412548";
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

DEMO

var jsonObj = [{"devices":{"dID":"TLSM01"},"uuid":"e863c776-f939-4761-bbce-bf0501b42ef7"},
 {"devices":{"dID":"TLSM01"},"uuid":"5a0cd70d-891d-48d8-b205-e92e828ac445"}];
 
jsonObj.map(item => item.devices["EntityID"] = "12458412548");

console.log(jsonObj);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123