3

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?

2
  • can you supply the value of data before you start manipulating it? as referenced in your .map() function? Commented May 20, 2015 at 8:14
  • are you sure, you want to use map() for this task? Commented May 20, 2015 at 8:16

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

you assign wrong object, seems it would be like result[item.category][item.key]=item.value and also result[item.category] = {} if it not exist
@Grundy You are correct. I fixed and added comments.
Thank you Madara Uchiha.. :)
2

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;
}, {});

Comments

2

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.