用数字名称调用JSON对象?

时间:2019-05-03 23:40:20

标签: javascript jquery arrays json object

例如:

{
  "123": [
     ...stuff
  ]
}

如何在jquery中调用“ 123”而不返回错误?

我尝试过:

$.getJSON('insert-url-here').then(function (data) { console.log(data.123.length) }

但它不起作用。

1 个答案:

答案 0 :(得分:0)

使用方括号表示法:

const data = {
  "123": [1, 2, 3]
}

console.log(data[123].length);

或者因为从技术上讲它是字符串:

const data = {
  "123": [1, 2, 3]
}

console.log(data["123"].length);