2

I have a variable that is returning an array of arrays, with each item in each array in double quotes.

var arrayOfArrays = [
  [ "Name", "Age", "Address" ],
  [ "A", "43", "CA" ],
  [ "B", "23", "VA" ],
  [ "C", "24", "NY" ]
]

I need to convert this to the following:

var arrayOfObjects = [
  {"Name":"A", "Age":"43", "Address":"CA"},
  {"Name":"B", "Age":"23", "Address":"VA"},
  {"Name":"C", "Age":"24", "Address":"NY"}
]
2
  • Not AngularJS related, removed tags Commented Mar 20, 2017 at 4:30
  • Hi I have a little tricky situation but little bit similiar to it [ [ { name: 'item_label_id', value: '000431a0-cf95-4ac9-af52-2a66f929a25f' } ], [ { name: 'item_label_id', value: '000563b6-de35-40e6-9c27-faa4416cd67b' } ]] I want this Array of Arrays like this {"name":"item_label_id","value":"'000431a0-cf95-4ac9-af52-2a66f929a25f'","Name":"xyz","Value":"234"} Do you have any suggestion? Commented Oct 21, 2020 at 16:35

2 Answers 2

10

Update

Less verbose version using Array.prototype.reduce() with newer language constructs like destructuring and computed property names:

const arrays = [
  ["Name", "Age", "Address"],
  ["A", "43", "CA"],
  ["B", "23", "VA"],
  ["C", "24", "NY"]
];

const [keys, ...values] = arrays;
const objects = values.map(array => array.reduce((a, v, i) => ({...a, [keys[i]]: v}), {}));

console.log(JSON.stringify(objects));


Original answer

Using Array.prototype.slice(), Array.prototype.map() and Array.prototype.forEach():

const arrays = [
  ["Name", "Age", "Address"],
  ["A", "43", "CA"],
  ["B", "23", "VA"],
  ["C", "24", "NY"]
];

const keys = arrays[0];
const values = arrays.slice(1);
const objects = values.map(array => {
  const object = {};

  keys.forEach((key, i) => object[key] = array[i]);
  
  return object;
});

console.log(JSON.stringify(objects));

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

6 Comments

Hello Robby, Thanks for your quick response. I am not using Angular2, could you please post the answer in angular js?
This is pure JavaScript and has nothing to do with AngularJS or Angular 2. Btw, you yourself added the angular2 tag to your question.
Thank you, Robby! Do you have any idea of doing this in Angular ?
Do you have any idea of doing this in Angular ?
What's the difference? JavaScript is JavaScript, no?
|
-1

Hope this helps

var arrayOfArrays = [
      [ "Name", "Age", "Address" ],
      [ "A", "43", "CA" ],
      [ "B", "23", "VA" ],
      [ "C", "24", "NY" ]
    ]
var arrayOfObjects = [];

function changeToObject(arrayOfArrays){  
    for (var i = 1; i < arrayOfArrays.length; i++) {
        var json = {
            "Name" : "",
            "Age" : "",
            "Address" : ""
        }
        json.Name = arrayOfArrays[i][0];
        json.Age = arrayOfArrays[i][1];
        json.Address = arrayOfArrays[i][2];
        arrayOfObjects.push(json);  
    };
    console.log(arrayOfObjects)

}

changeToObject(arrayOfArrays);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.