Anoop Chandran
Anoop Chandran

Reputation: 107

change JSON structure using javascript

This is my code..

var result = data.map(function(item){
    return {
        category:item.category,
        key:item.key,
        value:item.value
    }
});
console.log(result);

This is what is getting printed out in console..

Array[4]
0: Object
category: "common"
key: "Food"
value: "food"

1: Object
category: "welcome"
key: "title"
value: "Welcome..."

2: Object
category: "welcome"
key: "app_description"
value: "In this App "

3: Object
category: "welcome"
key: "select_location"
value: "Select Location"

This is what I'm trying to achieve

{
    common:{
        "Food" : "food"
    },
    welcome:{
        title : Welcome...,
        app_description : "In this App",
        select_location : "Select Location"

    }
}

This is the code I'm trying .but it is not working..

return {
    item.category:{
        item.key:item.value;                    
    }

Can anyone help me with this? I dont want to use GSON or any other third-party JS..How can i get this done using only core JS?

Upvotes: 3

Views: 1080

Answers (3)

Yangguang
Yangguang

Reputation: 1785

you can also try:

var data = [
    {
        category: "common",
        key: "Food",
        value: "food"
    },
    {
        category: "welcome",
        key: "title",
        value: "Welcome..."
    },
    {
        category: "welcome",
        key: "app_description",
        value: "In this App "
    },
    {
        category: "welcome",
        key: "select_location",
        value: "Select Location"
    }
];
var obj = {};
for (var i in data) {
    if(!obj[data[i].category]){
        obj[data[i].category] = {};
    }
    obj[data[i].category][data[i].key] = data[i].value;
}
console.log(obj);

Upvotes: 2

xdazz
xdazz

Reputation: 160833

Array.prototype.reduce() comes to rescue.

var result = data.reduce(function (previousValue, currentValue) {
    previousValue[currentValue.category] = previousValue[currentValue.category] || {};
    previousValue[currentValue.category][currentValue.key] = currentValue.value;
    return previousValue;
}, {});

Upvotes: 2

Madara's Ghost
Madara's Ghost

Reputation: 174947

First of all, what you want as a result is an object, not an array. So you can't use .map() which only maps one array to another array.

You want .reduce().

var result = data.reduce(function (result, item) {
    // Create the object the first time it arrives
    result[item.category] = result[item.category] || {};
    // Add the field
    result[item.category][item.key]=item.value;
    // Return the resulting object
    return result;
}, {});
console.log(result);

.reduce()'s reduction function takes two (with 2 more optional) parameters. The cumulative result, and the current item. The returned value is the result after the current item has been processed on it.

The second parameter for .reduce() (the first being the reduction function), is the initial value, this value will get passed as result for the first iteration.

Upvotes: 4

Related Questions