0

I am trying to access each object through a foreach...and I don't know how exactly because in that json array is a key for which there is a certain number of records and then another key with other values, etc. And if you noticed, there is also a color. How do I display the color in a json? I need that on my page.

This is my json:

{
  "7": [{
      "idType": 0,
      "nrReq": 41,
      "dataD": "2017-05-14",
      "typeC": "CO",
      "startDate": "2017-05-16",
      "endDate": "2017-05-17",
      "dataA": "2017-05-14",
      "color": "000080"
    }, {
      "idType": 0,
      "nrReq": 42,
      "dataD": "2017-05-14",
      "typeC": "CM",
      "startDate": "2017-05-23",
      "endDate": "2017-05-24",
      "dataA": "2017-05-18",
      "color": "000080"
    },

    "8": [{
      "idType": 0,
      "nrC": 53,
      "dataD": "2017-05-20",
      "typeC": "CO",
      "startDate": "2017-05-23",
      "endDate": "2017-05-23",
      "dataA": "2017-05-20",
      "color": "ffd9b3"
    }],

    "25": [{
      "idType": 0,
      "nrC": 51,
      "dataD": "2017-05-23",
      "typeC": "CP",
      "startDate": "2017-05-29",
      "endDate": "2017-05-30",
      "dataA": "2017-05-20",
      "color": "ff6600"
    }]
  }

How should it look? I think it should be 2 foreachs...

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Alexa
  • 61
  • 5

1 Answers1

0

You could use one, since the data ha a structure that you know about.

Notice: I might have assumed that there can be only one object "per id" cause of the syntax error in your JSON. :)

If, you dont know that there will only be one item in the array, you need to iterate each array (so 2 loops).

var data = {
  "7": [{
    "idType": 0,
    "nrReq": 41,
    "dataD": "2017-05-14",
    "typeC": "CO",
    "startDate": "2017-05-16",
    "endDate": "2017-05-17",
    "dataA": "2017-05-14",
    "color": "000080"
  }],
  "8": [{
    "idType": 0,
    "nrC": 53,
    "dataD": "2017-05-20",
    "typeC": "CO",
    "startDate": "2017-05-23",
    "endDate": "2017-05-23",
    "dataA": "2017-05-20",
    "color": "ffd9b3"
  }],

  "25": [{
    "idType": 0,
    "nrC": 51,
    "dataD": "2017-05-23",
    "typeC": "CP",
    "startDate": "2017-05-29",
    "endDate": "2017-05-30",
    "dataA": "2017-05-20",
    "color": "ff6600"
  }]
};

for (var id in data) {
  if (data.hasOwnProperty(id)) {
    console.log(data[id][0].color); // etc
  }
}
Thomas Wikman
  • 696
  • 4
  • 11
  • 1
    what about second element in `data['7']` array ? Have to assume arrays are of unknown length – charlietfl May 28 '17 at 20:07
  • Well, the JSON is invalid, so easy to assume that there was a missing property (id-ish). :) Also edited the answer to reflect that. – Thomas Wikman May 28 '17 at 20:10
  • Here, I fixed your json and made a code for you to read those props and get the color values. http://jsbin.com/qexakafago/edit?js,console,output Basically those number props are invalid key values of json data we used const props = Object.keys(json); to let javascript dynamically contain these properties. Then we needed a seperate variable (which is i) to create a 2nd route to the 2nd nested properties. – Chris May 28 '17 at 20:16