Convert array of array into array of object with custom keys- React

Viewed 75

I am currently working on a array of array received from an endpoint and I want it to be converted into array of object. My array looks like bellow,

{
  "results": [
    [
      "ASIA",
      "INDIA",
      "2003",
      "Type 1",
      "Group 1",
      "Division 1"
    ],
    [
      "AFRICA",
      "Nigeria",
      "2004",
      "Type 2",
      "Group 2",
      "Division 2"
    ]
}

Code I have tried so far :

let finalArr = [];

  var objs = data.results.map((val) => {
    finalArr.push(Object.assign({}, val));
  });

console.log("Converted into Array of Object",finalArr)

The above code returns array of object with keys as 0,1,2,3 but ,I would like my array object to have custom keys and expected output is :

[
    {
      Region: "ASIA",
      Country: "INDIA",
      Year: "2004",
      Type: "Type 1",
      Group: "Group 1",
      Divison: "Division 1",
    },
    {
      Region: "AFRICA",
      Country: "Nigeria",
      Year: "2003",
      Type: "Type 2",
      Group: "Group 2",
      Divison: "Division 2",
    },
  ];

8 Answers

You can do it using the map function like this:

  const data = {
    results: [
      ["ASIA", "INDIA", "2003", "Type 1", "Group 1", "Division 1"],
      ["AFRICA", "Nigeria", "2004", "Type 2", "Group 2", "Division 2"]
    ]
  };
  const finalArr = data.results.map((el) => ({
    Region: el[0],
    Country: el[1],
    Year: el[2],
    Type: el[3],
    Group: el[4],
    Divison: el[5]
  }));
  console.log(finalArr);

or with custom keys:

  const customKeys = ["Region", "Country", "Year", "Type", "Group", "Division"];
  const data = {
    results: [
      ["ASIA", "INDIA", "2003", "Type 1", "Group 1", "Division 1"],
      ["AFRICA", "Nigeria", "2004", "Type 2", "Group 2", "Division 2"]
    ]
  };
  const finalArr = data.results.map((el) => {
    const obj = {};
    for (let i = 0; i < el.length; ++i) {
      obj[customKeys[i]] = el[i];
    }
    return obj;
  });
  console.log(finalArr);

You can try this piece of code

let keys = ["Region", "Country", "Year", "Type", "Group", "Division"];

const result = [
  ["ASIA", "INDIA", "2003", "Type 1", "Group 1", "Division 1"],
  ["AFRICA", "Nigeria", "2004", "Type 2", "Group 2", "Division 2"]
];


let final = [];

result.forEach(elem => {
  let obj = {}
  elem.forEach((param,i )=> {
      obj[keys[i]] = param
  });
  final.push(obj)
});


console.log(final)

Try This boss. If you are sure the data returned is in the right order of the country and so on

let finalArr = [];

  var objs = data.results.map((val) => {
     const obj = {
      Region: val[0],
      Country: val[1],
      Year: val[2],
      Type: val[3],
      Group: val[4],
      Divison: val[5],
    }
    return obj
  });
finalArray = objs

Since the returned values are in the array, It okay to get those return values using there indices

using Object.fromEntries and an array of keys makes this trivial

const data = {results: [["ASIA", "INDIA", "2003", "Type 1", "Group 1", "Division 1"],["AFRICA", "Nigeria", "2004", "Type 2", "Group 2", "Division 2"]]};

const keys = ["Region", "Cuntry", "Year", "Type", "Group", "Division"];
const result = data.results.map((el) =>
  Object.fromEntries(keys.map((key, i) => [key, el[i]]))
);

console.log(result);

You could do something like this, For example:

let arrs = [
    [1, 2],
    [3, 4],
    [5, 6],
    [7, 8]
];

let objs = arrs.map(x => ({ 
  key1: x[0], 
  key2: x[1] 
}));

console.log(objs);
This is dynamic solution with custom keys.

const data = [
  [
    "ASIA",
    "INDIA",
    "2003",
    "Type 1",
    "Group 1",
    "Division 1"
  ],
  [
    "AFRICA",
    "Nigeria",
    "2004",
    "Type 2",
    "Group 2",
    "Division 2"
  ],
];
const keys = ["Region", "Country", "Year", "Type", "Group", "Division"];

const finalArr = data.map((item) => {
  const _obj = {};
  for (let i = 0; i < item.length; ++i) {
    _obj[keys[i]] = item[i];
  }
  return _obj;
});
console.log(finalArr);

let data = {
  "results": [
    [
      "ASIA",
      "INDIA",
      "2003",
      "Type 1",
      "Group 1",
      "Division 1"
    ],
    [
      "AFRICA",
      "Nigeria",
      "2004",
      "Type 2",
      "Group 2",
      "Division 2"
    ]
  ]
};

//custom keys array 
let keys = ["Region", "Country", "Year", "Type", "Group", "Division"];

let finalArr = data.results.map((obj) => {
  //create array of key and value map for each object
  let keyValMap = keys.map((k, i) => [k, obj[i]]);
  return Object.fromEntries(keyValMap);
});

console.log("Converted into Array of Object", finalArr)

Try with below code snippet

const data =  [
      ["ASIA", "INDIA", "2003", "Type 1", "Group 1", "Division 1"],
      ["AFRICA", "Nigeria", "2004", "Type 2", "Group 2", "Division 2"]
    ]
  
  const expectedOutput = data.map((el,i) => ({
    Region: el[i],
    Country: el[i],
    Year: el[i],
    Type: el[i],
    Group: el[i],
    Divison: el[i]
  }));
  console.log(expectedOutput);
Related