0

I need to turn this JSON object:

[
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ],
    [
        "[email protected]"
    ]
]

Into this:

{
    "data": [
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        }
] }

How is this done?

1

3 Answers 3

1

Well, that's really just an array of arrays, but that's besides the point. Just loop through the array of arrays and push the relevant data onto the new array in your object:

var my_new_object = {};
my_new_object['data'] = [];

for (var i = 0; i < your_array.length; i++) {
  my_new_object.data.push({"email": your_array[i][0]});
}

Working demo.

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

2 Comments

You're close, but I think you need: your_array[i][0]
Its very close. The [] around the email address should not be there. How can I remove it? pastie.org/1812997.
1

Javascript:

var original = [
[
    "[email protected]"
],
[
    "[email protected]"
],
[
    "[email protected]"
],
[
    "[email protected]"
]
];

var output = {};
output.data = new Array();
for(var i=0;i<original.length;i++){
    var o = new Object();
    o.email = original[i][0];
    output.data.push(o);
}

Comments

0

you can test it here: http://jsfiddle.net/v3fnk/

var data=[["[email protected]"],["[email protected]"],["[email protected]"],["[email protected]"]];

for (var i in data) {
    for (var x in data[i]) {
        $("#info").append(data[i][x] + '<br/>');
    }

}

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.