delliottg
delliottg

Reputation: 4150

How to access an object deep inside another object

I have an object that has a coefficients property that's buried six levels deep in the outer object.
I need to pass the entire object to a PHP file on our backend to process it for entries into our MySQL database.
I don't seem to be able to get the entire object by using deep cloning (using LoDash), or spread operator {...} or JSON.parse(JSON.stringify(obj));.
In every case I end up with just a blank set of square brackets where the data I'm interested in lives.
What I end up with is this (see the last blank entry for coefficients):

{
  "dostuff": "saveCalibrationStart",
  "debug": true,
  "configurationID": 0, 
  "computername": "REDACTED",
  "username": "REDACTED",
  "notes": "none",
  "softwarename": "CalRun",
  "softwareversion": "V12.0.0 beta > 1.13",
  "systemid": "42",
  "devices": [
    {
      "modelNumberID": "12",
      "serialNumber": "09998",
      "position": 1
    }
  ],
  "references": [
    {
      "modelNumberID": "12",
      "calibrationDeviceTypeID": 11,
      "serialNumber": "09999",
      "measurands": [
        {
          "measurementTypeID": "13",
          "measurand": "Phenanthrene",
          "calibrationDate": "2019-01-01",
          "coefficients": []
        }
      ]
    }
  ]
}

This guy's page on the spread operator seemed promising, but I'm getting the same thing: https://flaviocopes.com/javascript-spread-operator/

Further experimentation indicates that no matter what I do, I end up with the empty array for the coefficients, when they should looks like this:

Coefficients inside outer object

If I save the object as a global variable (Chrome), I am able to access, change and manipulate the elements of the coefficients like this:

temp1.references[0].measurands[0].coefficients

This seems like a simple task, grab the object, turn it into JSON & hand it off to the backend for processing, but it's been vexing me for a couple of days.

Upvotes: 0

Views: 70

Answers (2)

Inch High
Inch High

Reputation: 845

As requested by the OP, I'll try to provide what I was suggesting as an answer.

You appear to be declaring coefficients as an array, but whereever you appear be populating coefficients you are treating it as an object.

If you are using .push you may be doing something like:

coefficients.push(
drift: value,
offset: value,
etc etc
)

I believe, all you'll need to do where you are populating it is wrap the .push data into an actual object.

coefficients.push({
drift: value,
offset: value,
etc etc
})

I hope this helps, its difficult to offer you the exact solution without seeing where coefficients is populated.

Upvotes: 1

TKoL
TKoL

Reputation: 13932

If I take your code snippet and set the coefficients equal to [1,2,3,4], it works:

const temp1= {"dostuff":"saveCalibrationStart","debug":true,"configurationID":0,"computername":"REDACTED","username":"REDACTED","notes":"none","softwarename":"CalRun","softwareversion":"V12.0.0 beta 1.13","systemid":"42","devices":[{"modelNumberID":"12","serialNumber":"09998","position":1}],"references":[{"modelNumberID":"12","calibrationDeviceTypeID":11,"serialNumber":"09999","measurands":[{"measurementTypeID":"13","measurand":"Phenanthrene","calibrationDate":"2019-01-01","coefficients":[1,2,3,4]}]}]};

console.log(temp1.references[0].measurands[0].coefficients);

But what you posted DOES have an empty array for the coefficients. I think you might be a little bit confused about what your data is and/or when it's getting populated

Upvotes: 1

Related Questions