JSON数组转换为多维数组

时间:2015-11-07 23:09:10

标签: javascript arrays json mongodb

我是Javascript的新手,并尝试将我从mongodb获取的数据转换为多维数组,如下例所示。如果这可能是直接数组中的值的简单转换,那么可以使用for循环完成,但不确定如何进行多维数组转换?

请帮忙。

[["p",26],["ne",5],["n",69]]

需要多维数组,如下所示:

import java.util.Scanner; import java.util.Vector; public class home { public static void main(String[] args) { Scanner getWord = new Scanner(System.in); char[] alphabet = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', }; String[] Binary = new String[] { "00001 ", "00010 ", "00011 ", "00100 ", "00101 ", "00110 ", "00111 ", "01000 ", "01001 ", "01010 ", "01011 ", "01100 ", "01101 ", "01110 ", "01111 ", "10000 ", "10001 ", "10010 ", "10011 ", "10100 ", "10101 ", "10110 ", "10111 ", "11000 ", "11001 ", "11010 ", }; String word; System.out.println("Type in what you want to convert into binary: (to exit type in 'quit')"); while(true) { Vector<String> wordBin = new Vector<String>(); word = getWord.next(); if(word == "quit") { break; } for(int a = 0; a < word.length(); a++) { for(int b = 0; b < 27; b++) { if(word.charAt(a) == alphabet[b]) { wordBin.addElement(Binary[b]); } } } System.out.println(); System.out.println("That in binary is: "); System.out.println(); for(int c = 0; c < wordBin.size(); c++) { System.out.println(wordBin.get(c)); } System.out.println(); System.out.println("What is the next word that you would like to type in: "); } System.out.println(); System.out.println("Hava a nice day"); } }

1 个答案:

答案 0 :(得分:3)

您可以通过Array.protoype.map放置数组,该数组将使用回调函数返回的内容替换数组中的每个值。在回调函数中,您可以返回对象的数组版本。

例如:

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

可以在MDN docs for Arrays上找到更多数组方法。