如何将JSON数组转换为JavaScript 2D数组 [英] how to convert JSON array into JavaScript 2D array

查看:57
本文介绍了如何将JSON数组转换为JavaScript 2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下JSON转换为JS 2D数组,但是我不确定如何在html中执行此操作。

I want to convert the following JSON into a JS 2D array, but I am not sure how to do this in html.

[{"fields": {"diameter": 23.0, "neighbourhood": "WEST END"}, "model": "hug.tree", "pk": 345}, 
 {"fields": {"diameter": 14.0, "neighbourhood": "MOUNT PLEASANT"}, "model": "hug.tree", "pk": 484}]

结果应类似于:

[[23.0, 'WEST END'], [14.0, 'MOUNT PLEASANT']]

非常感谢! p>

Thank you so much!

推荐答案

这将适用于字段的所有字段,不仅适用于直径 邻居

This will work with all fields of "fields", not only for diameter or neighborhood.

演示

var items = [{"fields": {"diameter": 23.0, "neighbourhood": "WEST END"}, "model": "hug.tree", "pk": 345}, {"fields": {"diameter": 14.0, "neighbourhood": "MOUNT PLEASANT"}, "model": "hug.tree", "pk": 484}];

var i = 0, result = [];

while(i < items.length){
    result.push([])
    for(var key in items[i].fields){
        result[result.length-1].push(items[i].fields[key])	
    }
    i++
}

document.write(JSON.stringify(result, null, 4));

这篇关于如何将JSON数组转换为JavaScript 2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆