0

Want to convert this nested array, TableData, to a set of objects which point to the values of the first array as the key in the key/value pair. However, I keep getting the error that I can't run a function within a loop. Any thoughts on how to remedy this?

const tableData = [
  ["first_name", "last_name", "city", "state"],
  ["Elisabeth", "Gardenar", "Toledo", "OH"],
  ["Jamaal", "Du", "Sylvania", "OH"],
  ["Kathlyn", "Lavoie", "Maumee", "OH"]
  ];

  function convertTable(arr){
  let returnArr= [];

  for (let arrSpot=0; arrSpot< arr[0].length; arrSpot++){
    if (arrSpot !== 0){
      returnArr.push(
        arr[0].reduce((accum,arrItem,index)=> {
          accum[arrItem] = arr[arrSpot][index];
          return accum;
        },{})
      );
    }
  }
  console.log(returnArr);
  • How are you invoking this method? – Taplar Apr 10 '20 at 17:41
  • 1
    Does this answer your question? [How to fix jslint error 'Don't make functions within a loop.'?](https://stackoverflow.com/questions/3037598/how-to-fix-jslint-error-dont-make-functions-within-a-loop) – Titus Apr 10 '20 at 17:41
  • `accum[arrItem] = arr[arrSpot][index]` This is not how to define a key/value pair? Or am I missing something? – Dabrowski Apr 10 '20 at 17:45
  • @Dabrowski you're missing the `{}` passed as the initial parameter to `reduce`. – Alnitak Apr 10 '20 at 17:46
  • I don't think I've ever used `reduce`, might play with the example so I can get my head around it. – Dabrowski Apr 10 '20 at 17:48
  • you need just a `}` to end the function. then call the function with `tableData` and voilà! – Nina Scholz Apr 10 '20 at 17:51
  • _"keep getting the error"_ - from where? From your browser, or something like `jslint` ? – Alnitak Apr 10 '20 at 17:52
  • @NinaScholz i don't think the error the OP reported is down to that - I expect the missing brace is just a copy and paste issue. – Alnitak Apr 10 '20 at 18:02
  • Here is a single liner for your ref... `var convertTable = ([ks,...vss]) => vss.map(vs => vs.reduce((r,v,i) => (r[ks[i]] = v, r), {}));` – Redu Apr 10 '20 at 18:27

1 Answers1

1

You can use array.shift to get the first row as the header of the array then iterate the rest of the row.

const tableData = [
  ["first_name", "last_name", "city", "state"],
  ["Elisabeth", "Gardenar", "Toledo", "OH"],
  ["Jamaal", "Du", "Sylvania", "OH"],
  ["Kathlyn", "Lavoie", "Maumee", "OH"],
];

function convertTable(arr) {
  const header = arr.shift();
  return arr.map((i) => {
    return {
      [header[0]]: i[0],
      [header[1]]: i[1],
      [header[2]]: i[2],
      [header[3]]: i[3],
    };
  });
}
console.log(convertTable(tableData));

For dynamic object:

const tableData = [
  ["first_name", "last_name", "city", "state"],
  ["Elisabeth", "Gardenar", "Toledo", "OH"],
  ["Jamaal", "Du", "Sylvania", "OH"],
  ["Kathlyn", "Lavoie", "Maumee", "OH"],
];

function convertTable(arr) {
  const header = arr.shift();
  return arr.map((i) => {
    return header.reduce((m, key, index) => {
      m[key] = i[index];
      return m;
    }, {});
  });
}
console.log(convertTable(tableData));
xdeepakv
  • 7,835
  • 2
  • 22
  • 32