0

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.

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
delliottg
  • 3,950
  • 3
  • 38
  • 52
  • 1
    Are the `coefficients` populated *later* by any chance? Because the behaviour you describe is consistent with [the lazy evaluation behaviour of the console](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – VLAZ Dec 19 '19 at 14:51
  • 2
    This looks like a mismatch between an array and an object? 'coefficients' seems to be an array as indicated by Array(0), yet when you've expanded it it seems to be an object with KV pairs? Edit - are you missing the surrounding {} when you're populating coefficients? – Inch High Dec 19 '19 at 14:55
  • @InchHigh OH, WELL SPOTTED! I completely missed that. Seems like OP is initialising an array but then treating it as an object. This will lead to exactly this outcome - when cloning arrays and random properties will not be copied across. – VLAZ Dec 19 '19 at 14:56
  • My colleague & I are investigating the lazy evaluation behavior right now. We're relatively sure that's what's going on, but haven't figured out why it's not populating when we expect that it is. The array / object mismatch is also a good path to investigate, thank you. – delliottg Dec 19 '19 at 15:41
  • @delliottg - Good stuff, as requested I've tried to provide a solution as to how I think you may resolve it. If the provided answer doesn't help, if you could show me how you're populating coefficients I may be able to better assist. – Inch High Dec 19 '19 at 15:57

2 Answers2

1

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

TKoL
  • 13,158
  • 3
  • 39
  • 73
  • I think he may just be missing his surrounding {} when hes populating coefficients. – Inch High Dec 19 '19 at 14:56
  • @InchHigh, your speculation about array / object notation appears to be correct, we were declaring the coefficients as an array and trying to use it as an object (and subsequently modifying the array prototype in the process). If you'd like to turn your comment into an answer, I'll mark it as such. – delliottg Dec 19 '19 at 15:52
1

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.

Inch High
  • 835
  • 5
  • 17