2

I have this json structure.

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

From this json structure, I want to produce the following json structure;

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. 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?

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"
    }]
}]
guagay_wk
  • 26,337
  • 54
  • 186
  • 295

4 Answers4

1

The other answers here (except @user2415266) are not dynamic, hard-coded to accept an exact input, and are not particularly reusable. 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'.

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);
jkdev
  • 11,360
  • 15
  • 54
  • 77
BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • 1
    I just edited the version with map into my answer as well. – Danmoreng Oct 23 '15 at 09:48
  • 1
    Why is it when you call `a = json1.map(restructureJson);`, you do not need to provide an argument into `restructureJson()`? When you look at the function declaration of `restructureJson()`, the function has one argument. – guagay_wk Oct 27 '15 at 07:49
  • 2
    Because the native JavaScript Array method of `map()` automatically invokes the function you've passed it with a number of arguments. i.e. As it processes your array elements and applies the function to each one, it automatically passes across the value of the element to the function for you. See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map – BadHorsie Oct 28 '15 at 18:41
0
var y1=[]; 
for(var i=0;i<x.length;i++){

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

}
Vibhesh Kaul
  • 2,593
  • 1
  • 21
  • 38
  • Thanks. It works. Is it possible to use map() function to do the iteration? I was looking for something more unusual. – guagay_wk Oct 23 '15 at 09:19
0

As a single statement:

y = x.map(function(e)
{
    return {
        c:
        [
            {
                v: e.value
            },
            {
                v: e.date_transacted
            }
        ]
    };
});
Siguza
  • 21,155
  • 6
  • 52
  • 89
0

Both other answers only work if the objects in x have the exact 2 properties. 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;
});
Danmoreng
  • 2,367
  • 1
  • 19
  • 32