JSON数组转化为多维数组 [英] JSON array conversion into multi dimension array

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

问题描述

我是新来的Javascript和努力,我从MongoDB的进入多维数组像下面的例子转换数据。如果这本来是直线排列值的简单转换比,这可能已经使用了循环,但不知道维数组如何转换多可以做?

I am new to Javascript and trying to convert data that I am getting from mongodb into multi dimension array like example below. If this could have been simple conversion of values in straight array than this could have been done using for loop but not sure how multi dimension array conversion can be done?

请帮忙。

[
    {
        "text": "p",
        "count": 26
    },
    {
        "text": "ne",
        "count": 5
    },
    {
        "text": "n",
        "count": 69
    }
] 

需要多维数组象下面这样:

Need multi dimension array like below:

[P,26],[NE,5],[N,69]

推荐答案

您可以将阵列通过的 Array.protoype.map ,该替换无论回调函数返回数组中的每个值。在回调函数就可以返回对象的数组版本。

You can put the array through Array.protoype.map, which replaces each value in the array with whatever the callback function returns. In the callback function you can return an array version of the object.

例如:

var result = yourArray.map(function (item) {
    return [item.text, item.count];
});

更多的阵列方法可以在 MDN文档中找到对于阵列

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

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