Q:

How to fetch field values from nested array?

I need to get the value and the axis.
Here is what I have.

var d = [
    [
        {axis:"Customer Service",value:0.6},
        {axis:"Data",value:0.6},
        {axis:"Network",value:0.6},
        {axis:"Product",value:0.6},
        {axis:"Specialized",value:0.6},
        {axis:"Threat",value:0.6},
    ],
    [
        {axis:"Customer Service",value:0.3},
        {axis:"Data",value:0.3},
        {axis:"Network",value:0.3},
        {axis:"Product",value:0.3},
        {axis:"Specialized",value:0.3},
        {axis:"Threat",value:0.3},
    ]

function myFunction() {
    var sample = d[0];
    alert(sample);

Is there a way I could get the output
Customer Service: 0.6

A:

When looking at a compound data structure the square brackets are going to represent arrays and the curly brackets objects. In this case there is an array of arrays which contains objects, and the objects need to be identified by their "axis".

var d = [
          [
            {axis:"Customer Service",value:0.6},
            {axis:"Data",value:0.6},
            {axis:"Network",value:0.6},
            {axis:"Product",value:0.6},
            {axis:"Specialized",value:0.6},
            {axis:"Threat",value:0.6},
          ],
          [
            {axis:"Customer Service",value:0.3},
            {axis:"Data",value:0.3},
            {axis:"Network",value:0.3},
            {axis:"Product",value:0.3},
            {axis:"Specialized",value:0.3},
            {axis:"Threat",value:0.3},
          ]
        ];
    
function myFunction() {
  let sample = '';
  let index = 0; // which of the array items is being looked at, if in a loop
  // this value could be incremented to get the Customer Service value from 
  // all of the entries in the outer array.
  d[index].forEach(function(dItem) { // loop through the array of a single object
    // looping through allows you to access any of the axis objects, not just the first
    if (dItem.axis === 'Customer Service') { // check which object
      sample = dItem.value; // if the desired object then set sample
    }
  });
  // alternatively check to see that sample is still not equal to '' before
  // alerting
  alert(sample);
}
myFunction();

