Skip to content
Advertisement

Javascript/jQuery to get subarray value with random array key

I have these below JSON data:

{
"0": {
    "jQuery331045811028719032642": {
        "stepCount": 1,
        "captionSize": 0,
        "countdown": true,
        "countdownAlertLimit": 10,
        "displayCaptions": false,
        "displayDays": 0,
        "displayHours": true,
        "fontFamily": "Verdana, sans-serif",
        "fontSize": 0,
        "lang": "en",
        "languages": {},
        "seconds": 2609,
        "start": true,
        "theme": "white",
        "width": 4,
        "height": 30,
        "gap": 11,
        "vals": [0, 0, 4, 3, 2, 9],
        "limits": [2, 9, 5, 9, 5, 9],
        "iSec": 5,
        "iHour": 1,
        "tickTimeout": 1000,
        "intervalId": 1,
        "tickCount": 0,
        "timeTo": "2021-06-12T15:14:00.000Z",
        "options": {
            "timeTo": "2021-06-12T15:14:00.000Z",
            "start": true,
            "theme": "white",
            "seconds": 2609
        },
        "sec": 2609,
        "ttStartTime": 1623508230144
    },
    "jQuery331045811028719032641": {
        "hasDataAttrs": true
    }
},
"length": 1
}

Assume above array variable is var data;

For this subarray jQuery331045811028719032642 is auto generate.

My question, how to get seconds array value using jQuery?

I tried this:

alert(data[0].seconds);

but it returns undefined.

Advertisement

Answer

You can get the seconds using Object.values

const data = {
  "0": {
    jQuery331045811028719032642: {
      stepCount: 1,
      captionSize: 0,
      countdown: true,
      countdownAlertLimit: 10,
      displayCaptions: false,
      displayDays: 0,
      displayHours: true,
      fontFamily: "Verdana, sans-serif",
      fontSize: 0,
      lang: "en",
      languages: {},
      seconds: 2609,
      start: true,
      theme: "white",
      width: 4,
      height: 30,
      gap: 11,
      vals: [0, 0, 4, 3, 2, 9],
      limits: [2, 9, 5, 9, 5, 9],
      iSec: 5,
      iHour: 1,
      tickTimeout: 1000,
      intervalId: 1,
      tickCount: 0,
      timeTo: "2021-06-12T15:14:00.000Z",
      options: {
        timeTo: "2021-06-12T15:14:00.000Z",
        start: true,
        theme: "white",
        seconds: 2609,
      },
      sec: 2609,
      ttStartTime: 1623508230144,
    },
    jQuery331045811028719032641: {
      hasDataAttrs: true,
    },
  },
  length: 1,
};
const zeroObj = data["0"];
const result = Object.values(zeroObj)[0].seconds;
console.log(result);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement