0

I have an array, like this:

["1", "[email protected]", "user111", "something1"],
["2", "[email protected]", "user222", "something2"],
["3", "[email protected]", "user333", "something3"],
...
["N", "[email protected]", "userNNN", "somethingN"]

How can I turn it into JSON like this:

{ 
  "order" : "1" 
, "email": "[email protected]" 
, "username": "user111" 
, "note": "something1" 
},
{ 
  "order" : "2"
, "email": "[email protected]"
, "username": "user222" 
, "note": "something2"
},
{ 
  "order" : "3" 
, "email": "[email protected]" 
, "username": "user333" 
, "note": "something3" 
}, 

... 

{ 
  "order" : "N" 
, "email": "[email protected]" 
, "username": "userNNN" 
, "note": "somethingN" 
}

I'm self-study JavaScript, so I appreciate if you can suggest a similar case or document to learn more.

Thank you.

1
  • I'm thinking about run a for loop through the inside array, take out the first item "1" and put "order" : "1" back, take out the second item "[email protected]" and put back "email" : "[email protected]" and so on, until I have a right "name : value" paired array, then use json.stringify() that array. Is it the good way to do? Commented Mar 17, 2016 at 23:01

3 Answers 3

1

You can do it in many ways, e.g. using a classic for loop.

The way I currently favor is using Array.map:

array = [
    ["1", "[email protected]", "user111", "something1"],
    ["2", "[email protected]", "user222", "something2"],
    ["3", "[email protected]", "user333", "something3"],
    ...
    ["N", "[email protected]", "userNNN", "somethingN"]
];

var arr2 = array.map(function(el, i) {
    return {
        order: el[0],
        email: el[1],
        username: el[2],
        note: el[3]
    };
});

See here for a reference. Lots and lots of goodies in there.

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

Comments

0

Something like this:

var arrayOfObjs = [];
for(var i in inputArray) {
    var data = inputArray[i];
    var obj = {
        order: data[0],
        email: data[1],
        username: data[2],
        note: data[3]
    };
    arrayOfObjs.push(obj);
}

console.log(arrayOfObjs);

You first loop through the array and since you know that the inner arrays are always of length 4 with [0 = order, 1 = email, 2 = username, 3 = note], you can safely create new objects and add them to a new array. You can also do this operation in array, which means that your input array gets updated with the new format (you basically do not create a new array):

inputArray[i] = obj; // replace line `arrayOfObjs.push(obj)` with this one

1 Comment

Thank you so much. The replace and update inputArray is easier for me to understand and use. So I accepted your answer. Have nice day!
0

In pure javaScript, I'd go for Chiesa's solution. If you'd like to use a libraray, here's a way to do it with underscore.js:

var new_array = _.map(array, function(entry) {
   return _.object(['order', 'email', 'username', 'note'], entry);
});

var json = JSON.stringify(new_array);

1 Comment

Yes, I agree that Chiesa's code is short, but I will use that way when I can understand JavaScript better. Thank you for your suggestion.

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.