我有一个来自我的API的JSON响应,其结构如下:

{
  "data": [
    {
      "id": "1", "name": "test"
    },
    {
      "id": "2", "name": "test2"
    }
  ]
}


当我引用数据时,每条记录都会得到一个数组。由于我使用的插件要求将其作为数组,因此我需要将花括号括起来。

所需的输出:

[
  ["1", "test"],
  ["2", "test"]
]


如何将上面的JSON转换为此呢?

编辑:

原来这是我使用的插件存在的问题,而且我一直都知道如何做到这一点。以为我疯了,但我的代码很好,某些插件搞砸了。

最佳答案

您可以使用Array.prototype.map

var arr = json.data.map(function(x){
   return [x.id, x.name];
});

09-10 10:50
查看更多