0

I'm trying to convert an array of arrays that is requested from a http service. I have the following code below:

This is the array from the http service that will have a number of arrays:

[object Array] > [0] > MAKE: "toshiba", MODEL: "h2000" ..[n]

This needs to be converted to a multidimensional array like this:

[{MAKE:"toshiba"},{MODEL:"h2000"}, {MAKE:"HP"},{MODEL:""}];

I have looked into a for loop to do this but having no luck:

1 Answer 1

1

Create a method that converts an object to an array

function objToArray(obj) {
    return Object.keys(obj).reduce(function(arr, key) {
        arr.push({ [key] : obj[key] });
        return arr;
    }, []);
}

And then run your array through it:

var formattedArray = arr.map(x => objToArray(x));

Demo: https://jsfiddle.net/41d3p6zx/

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

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.