在没有jquery的情况下映射对象数组

时间:2016-06-14 21:59:02

标签: javascript arrays

我有一个数组

showsList = [{
    "location": "ELM",
    "date": 04 - 03 - 16,
    "stage": "BLUE",
    "name": "MotorBlur",
    "id": 106
}, {
    "location": "KRAFT",
    "date": 04 - 03 - 16,
    "stage": "ORANGE",
    "name": "donotdelete",
    "id": 107
}];

(有很多数据,但为了简单起见,你明白了)。我习惯于像这样映射数组...

{showList.map((show, idx) => (

但这并不适用,因为这些值不像普通数组值那样可以直接访问。如何映射数组以检索对象内的数据? (用jsx语法编写)感谢您提前帮助

2 个答案:

答案 0 :(得分:1)

您可以通过数组中的for循环访问它们,然后使用对象表示法,但如果您想使用map,请尝试以下操作:

showsList.map(function(obj){
  for(var item in obj){
    console.log("key: "+ item)
    console.log("value: " + obj[item]);
  }
});

这是example

答案 1 :(得分:0)

为什么你不能完全使用你在描述中写的内容:

showsList.map((show, idx) => {
    console.log(show.location)
    console.log(show.date)
    console.log(show.stage)
    console.log(show.name)
    console.log(show.id)
})