【问题标题】:Javascript/jQuery to get subarray value with random array keyJavascript/jQuery 使用随机数组键获取子数组值
【发布时间】:2021-06-13 12:25:08
【问题描述】:

我有以下 JSON 数据:

{
"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
}

假设上面的数组变量是var data;

对于这个子数组jQuery331045811028719032642 是自动生成的。

我的问题,如何使用 jQuery 获取 seconds 数组值?

我试过了:

alert(data[0].seconds);

但它返回未定义。

【问题讨论】:

  • 完整对象中没有seconds数组。

标签: javascript jquery json


【解决方案1】:

您可以使用Object.values 获取seconds

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);

【讨论】:

    【解决方案2】:

    您可以尝试使用此代码

    const arrayData = Object.values(data["0"]);
    console.log(arrayData[0].seconds);
    

    【讨论】:

      【解决方案3】:

      您可以尝试其中任何一个来获取秒值。

      1- 使用 Object.values 获取 Object 内部的值,如下所示

      let res = Object.values(data["0"]);
      let seconds = res.seconds; // This will give you value of seconds.
      

      2- 使用 Object.keys 获取对象的键并使用该键获取数据。

      let keys = Object.keys(data[0]);
      let seconds = data[0][keys[0]].seconds; 
      

      【讨论】:

        猜你喜欢
        • 2018-06-19
        • 2022-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多