简体   繁体   English

如何遍历这个 json 结构以生成另一个结构?

[英]How do I iterate over this json structure to produce another structure?

I have this json structure.我有这个 json 结构。

x=[{
    "value": 1.37,
    "date_transacted": "2015-01-01"
}]

From this json structure, I want to produce the following json structure;从这个 json 结构中,我想生成以下 json 结构;

y1=[{
    c: [{
        v: "2015-01-01"
    },
    {
        v: "1.37"
    }]
}]

I have written the code to do this.我已经编写了代码来做到这一点。 It looks like this;它看起来像这样;

var y1 = [{ c:[ {"v":x[0].value}, {"v":x[0].date_transacted} ] }];

My problem comes when x has several json key/value pairs.x有几个 json 键/值对时,我的问题就出现了。 Something that look like this;看起来像这样的东西;

x=[{
    "value": 1.37,
    "date_transacted": "2015-01-01"
},
{
    "value": 1.62,
    "date_transacted": "2015-02-01"
},
{
    "value": 1.83,
    "date_transacted": "2015-03-01"
}]

What is an effective way to iterate my code through the array of objects to produce the desired json structure which should look like this?通过对象数组迭代我的代码以生成应如下所示的所需 json 结构的有效方法是什么?

y=[{
    c: [{
        v: "2015-01-01"
    },
    {
        v: "1.37"
    }]
},
{
    c: [{
        v: "2015-01-02"
    },
    {
        v: "1.62"
    }]
},
{
    c: [{
        v: "2015-01-03"
    },
    {
        v: "1.83"
    }]
}]

The other answers here (except @user2415266) are not dynamic, hard-coded to accept an exact input, and are not particularly reusable.此处的其他答案(@user2415266 除外)不是动态的、硬编码的以接受确切的输入,并且不是特别可重用。 They will fail if you have more than 2 properties, or in @Siguza's case, also if you have properties not called 'date_transacted' and 'value'.如果您有 2 个以上的属性,或者在@Siguza 的情况下,如果您有未称为“date_transacted”和“value”的属性,它们将失败。

function restructureJson(obj) {
    var output = {c:[]};
    for (var i in obj) {
        output.c.push({v:obj[i]});
    }
    return output;
}

This function is reusable on any array of objects, of any size, containing any number of properties.此函数可在任何大小、包含任意数量属性的任何对象数组上重用。

// Simple example
var json1 = [{
    "value": 1.37,
    "date_transacted": "2015-01-01"
}];

// More complex
var json2 = [{
    "value": 1.37,
    "date_transacted": "2015-01-01",
    "another_value": "test",
    "more": "12356"
},
{
    "value": 1.62
},
{
    "value": 1.83,
    "date_transacted": "2015-03-01",
    "stuff": "124334654567"
}];

// Map the function to the arrays
a = json1.map(restructureJson);
b = json2.map(restructureJson);
var y1=[]; 
for(var i=0;i<x.length;i++){

   y1[i]= [{ c:[ {"v":x[i].value}, {"v":x[i].date_transacted} ] }];

}

As a single statement:作为一个单一的声明:

y = x.map(function(e)
{
    return {
        c:
        [
            {
                v: e.value
            },
            {
                v: e.date_transacted
            }
        ]
    };
});

Both other answers only work if the objects in x have the exact 2 properties.其他两个答案仅在 x 中的对象具有精确的 2 个属性时才有效。 A way to do it regardless of the amount of properties:无论属性数量如何,都可以这样做:

y = [];
for(var i = 0, i < x.length; i++){
    var obj = {c:[]};
    for(var prop in x[i]){
        obj.c.push({v:x[i][prop]})
    }
    y.push(obj);
}

Edit: with map编辑:带地图

y = x.map(function(e)
{
    var obj = {c: []};
    for(var prop in e){
        obj.c.push({v:e[prop]})
    }
    return obj;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM