3

How can I convert this:

var expenseList = [[1,"Beverages"],
                   [2,"Condiments" ],
                    [3,"Confections" ],
                   [4,"Dairy Products" ],
                   [5,"Grains/Cereals" ],
                   [6,"Meat/Poultry" ],
                    [7,"Produce" ],
                   [8,"Seafood" ]];

into this:

output = [
               { value: 1, text: "Beverages" },
               { value: 2, text: "Condiments" },
               { value: 3, text: "Confections" },
               { value: 4, text: "Dairy Products" },
               { value: 5, text: "Grains/Cereals" },
               { value: 6, text: "Meat/Poultry" },
               { value: 7, text: "Produce" },
               { value: 8, text: "Seafood" }
        ];

The first data source can be taken as input and the second is the required output. I tried converting the array into a type of string using a loop and then parse the string into json but Json.pasre throws error there.

var list = '';
for (var i = 0; i < expenseList.length; i++) {
        var showText = expenseList[i][1].replace('"', '\\"');
        var key = expenseList[i][0];

    list = '{ value: ' + key + ', text: "' + value + '"},' + list;
}

    list = '[' + list.substr(0, list.length - 1) + ']';
    var bindList;
    bindList = JSON.parse(list);
2
  • Trying to build up a JSON object as a string is very error-prone. Commented Apr 11, 2016 at 14:20
  • There are exactly 0 (zero) "JSON objects" in your required output. Or anything else remotely JSON-related for that matter. Commented Apr 11, 2016 at 14:20

5 Answers 5

7

simply try

var output = expenseList.map(function(val){
  return { value: val[0], text: val[1] }
}); 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array.prototype.map

var list = expenseList.map(function(x) {
  return {
     value: x[0],
     text: x[1]
  };
});

To then turn into JSON you can use

var json = JSON.stringify(output);

Comments

0

Here is an alternative solution that builds up the array in the way you were trying, but is more correct:

var expenseList = [[1,"Beverages"],
                   [2,"Condiments" ],
                   [3,"Confections" ],
                   [4,"Dairy Products" ],
                   [5,"Grains/Cereals" ],
                   [6,"Meat/Poultry" ],
                   [7,"Produce" ],
                   [8,"Seafood" ]];

var list = [];
for (var i = 0; i < expenseList.length; i++) {
    var val = expenseList[i][0];    
    var txt = expenseList[i][1];

    list.push({value: val, text: txt});
}

console.log(list);

Comments

0

If you don't want an array and hash object meets your requirement then use this.

loadash used.

_.zipObject(_.map(expenseList,0),_.map(expenseList,1))

Output will be

{1: "Beverages", 2: "Condiments", 3: "Confections", 4: "Dairy Products", 5: "Grains/Cereals", 6: "Meat/Poultry", 7: "Produce", 8: "Seafood"}

Comments

0

One liner solution using .map

var expenseList = [[1,"Beverages"],[2,"Condiments" ],[3,"Confections" ],[4,"Dairy Products" ],[5,"Grains/Cereals" ],[6,"Meat/Poultry" ],[7,"Produce" ],[8,"Seafood" ]],
      output = expenseList.map(([value,text])=>({value:+value,text})); 

console.log(output);

1 Comment

What is change in your answer then others

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.