在循环“for”中创建一个变量

时间:2017-04-19 14:27:27

标签: javascript

我正在处理一个函数,用于将数组创建为数组。

我有一个像这样的JSON:

[
    [
        {"data_name":"dn1","defaut_name":"1F-27","occurrence":5},
        {"data_name":"dn1","defaut_name":"20-34","occurrence":1}
    ],[
        {"data_name":"bm1","defaut_name":"13-02","occurrence":4}
    ],[
        {"data_name":"dn2","defaut_name":"1F-27","occurrence":3},
        {"data_name":"dn2","defaut_name":"20-34","occurrence":1}
    ],[
        {"data_name":"bm2","defaut_name":"13-02","occurrence":4}
    ],[
        {"data_name":"bm3","defaut_name":"13-02","occurrence":3}
    ],[
        {"data_name":"bm4","defaut_name":"13-02","occurrence":3}
    ]
]

我想得到这样的结果:

[
    [5,4,3,4,3,3],
    [1,0,1,0,0,0]
]

这对应于事件的串联。

我尝试使用循环,但它不起作用。

var data = [];
for (var i in chartData) {
    var array = [];
    for (var j in chartData[i]) {
        array.push(chartData[i][j].occurrence)
    }
    data.push(array);
}

感谢您的帮助。

4 个答案:

答案 0 :(得分:0)

您可以简单地使用您所需的结果来制作您想要的输出:



var chartData = [
    [
        {"data_name":"dn1","defaut_name":"1F-27","occurrence":5},
        {"data_name":"dn1","defaut_name":"20-34","occurrence":1}
    ],[
        {"data_name":"bm1","defaut_name":"13-02","occurrence":4}
    ],[
        {"data_name":"dn2","defaut_name":"1F-27","occurrence":3},
        {"data_name":"dn2","defaut_name":"20-34","occurrence":1}
    ],[
        {"data_name":"bm2","defaut_name":"13-02","occurrence":4}
    ],[
        {"data_name":"bm3","defaut_name":"13-02","occurrence":3}
    ],[
        {"data_name":"bm4","defaut_name":"13-02","occurrence":3}
    ]
];

var data = [];
for (var i in chartData) {
    var array = [];
    for (var j in chartData[i]) {
        array.push(chartData[i][j].occurrence)
    }
    data.push(array);
}

var newData = [ [], [] ];
for (var i in data) {
    newData[0].push(data[i][0]);
    newData[1].push(data[i][1] || 0);
}

console.log(newData);




答案 1 :(得分:0)

这应该可以解决问题。

let arr = [
    [
        {"data_name":"dn1","defaut_name":"1F-27","occurrence":5},
        {"data_name":"dn1","defaut_name":"20-34","occurrence":1}
    ],[
        {"data_name":"bm1","defaut_name":"13-02","occurrence":4}
    ],[
        {"data_name":"dn2","defaut_name":"1F-27","occurrence":3},
        {"data_name":"dn2","defaut_name":"20-34","occurrence":1}
    ],[
        {"data_name":"bm2","defaut_name":"13-02","occurrence":4}
    ],[
        {"data_name":"bm3","defaut_name":"13-02","occurrence":3}
    ],[
        {"data_name":"bm4","defaut_name":"13-02","occurrence":3}
    ]
]

/* this could be done more elegantly */
let l1 = arr.map( (t) => t[0]!==undefined ? t[0]["occurrence"]:0);
let l2 = arr.map( (t) => t[1]!==undefined ? t[1]["occurrence"]:0);

let result = [l1,l2];

答案 2 :(得分:0)

您可以尝试使用map功能将输入转换为所需的输出。

var obj =[
    [
        {"data_name":"dn1","defaut_name":"1F-27","occurrence":5},
        {"data_name":"dn1","defaut_name":"20-34","occurrence":1}
    ],[
        {"data_name":"bm1","defaut_name":"13-02","occurrence":4}
    ],[
        {"data_name":"dn2","defaut_name":"1F-27","occurrence":3},
        {"data_name":"dn2","defaut_name":"20-34","occurrence":1}
    ],[
        {"data_name":"bm2","defaut_name":"13-02","occurrence":4}
    ],[
        {"data_name":"bm3","defaut_name":"13-02","occurrence":3}
    ],[
        {"data_name":"bm4","defaut_name":"13-02","occurrence":3}
    ]
];

var result1 = obj.map(x=>x[0].occurrence);
var result2 = obj.map(x => x[1] ? x[1].occurrence : 0);                         

var result = [result1 , result2];
console.log(result);

答案 3 :(得分:0)

JavaScript不支持真正的多维数组,但您可以使用数组数组来近似它们。要访问数组数组中的值,只需使用[]运算符两次。

// Created a multinational array 
var table = new Array(10);
for(var i = 0; i < table.length; i++)
       table[i] = new Array(10);

// Initialize the array
for(var row = 0; row < table.length; row++){
    for(col = 0; col < table[row].length; col++){
        table [row][col] = row*col;
    }
}

// Use the multidimensional array to compute 2*5
var product = table [2][5];   // 10