数字作为对象键,值作为数组

时间:2019-09-27 06:59:23

标签: javascript reactjs

该对象怎么了?

const ob= {
  "1": [
    {
      "name": "destination"
   }
  ]
}, {
  "2": [
    {
      "name": "destination"
    }
  ]
}

为什么我不能做ob['1']ob['2']?对象的值不能包含数组?

2 个答案:

答案 0 :(得分:2)

您的对象结构不正确。您需要将12放在对象ob的同一级别上:

const ob = {
  "1": [{
    "name": "destination"
  }],

  "2": [{
    "name": "destination"
  }]
};
console.log(ob['1']);
console.log(ob['2']);

答案 1 :(得分:1)

对象的结构无效。右括号弄乱了。

const ob = {
  "1": [{
    "name": "destination",

  }],
  "2": [{
    "name": "destination",

  }]
}

console.log(ob['1']);
console.log(ob['2']);