4

I am new to coding and trying to get this problem right. I tried calling the index of the arrays but it prints out columns. Out dev environment is REPL.it, the virtual machine uses node v10.16.0 and help would be greatly appreciate

Input

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

Desired Output

output = [
    { first_name : "Elisabeth", last_name : "Gardenar", city: "Toledo", state : "OH" },
    { first_name : "Jamaal", last_name : "Du", city: "Sylvania", state : "OH" },
    { first_name : "Kathlyn", last_name : "Lavoie", city: "Maumee", state : "OH" }
]

What I have tried so far is this:

function convertTable(){
    var p=console.log;
    const [header,...rows] = tableData;
    var tableObj = new Object;
    var intermediateVal = [];
    var finalArr = [];
    for(var vals=0;vals<rows.length;vals++){
        var row = rows[vals]
        for (var key=0,val=0;key<header.length;key++,val++){
            tableObj[header[key]]=row[val]
        }
    p(tableObj)
  }
} 
convertTable(tableData)

4 Answers 4

6

You're pretty close, but you need to create a new object for each row of the result, and then push that object onto the finalArr array.

There's also no need for both key and val variables, since they're always the same.

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

function convertTable() {
  var p = console.log;
  const [header, ...rows] = tableData;
  var finalArr = [];
  for (var vals = 0; vals < rows.length; vals++) {
    var row = rows[vals]
    var tableObj = {};
    for (var key = 0; key < header.length; key++) {
      tableObj[header[key]] = row[key]
    }
    finalArr.push(tableObj);
  }
  p(finalArr)

}
convertTable(tableData)

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

Comments

4

Here is a potential solution that uses array reduce.

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

const keys = tableData.shift();

const formatted = tableData.reduce((agg, arr) => {
  agg.push(arr.reduce((obj, item, index) => {
    obj[keys[index]] = item;
    return obj;
  }, {}));
  return agg;
}, [])

console.log(formatted);

1 Comment

TL;DR : Thank you, this is great work. Acutal Comment: NICK!!!!!!! that is freaking amazing. I just find it hard to follow it. I am very new to programming. If my teacher compares my code to your code, they will know i cheated on the assignment. I tried Array.shift() method on the rows. It did not occur to me to use it for the header. I was upset, "why can't i get the shift() to loop in the loop!!" Because it didn't make sense. However you are amazing.
0

You can also leverage the reduce method which is included in arrays.

tableData = [
    ["Elisabeth", "Gardenar", "Toledo", "OH"],
    ["Jamaal", "Du", "Sylvania", "OH"],
    ["Kathlyn", "Lavoie", "Maumee", "OH"]
];

const obj = tableData.reduce((acc, cv, index) => {
        acc.push({
            first_name: cv[0],
            last_name: cv[1],
            city: cv[2],
            state: cv[3]
        });
        return acc
},[]);

console.log(obj); 
// [ { first_name: 'Elisabeth',
//    last_name: 'Gardenar',
//    city: 'Toledo',
//    state: 'OH' },
//  { first_name: 'Jamaal',
//    last_name: 'Du',
//    city: 'Sylvania',
//    state: 'OH' },
//  { first_name: 'Kathlyn',
//   last_name: 'Lavoie',
//    city: 'Maumee',
//   state: 'OH' } ]

1 Comment

Thank you for your comment, however the Object Keys must be dynamic and not static. Otherwise I would use this method.
0

Another functional approach

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

const [keys, ...values] = tableData

const obtainValues = (line) => {
    const [first_name, last_name, city, state] = line
    return {first_name, last_name, city, state}
}

const output = values.map(obtainValues)

console.log(output)

See demo here

1 Comment

Thank you for your comment, however the Object Keys must be dynamic and not static. Map kind of confuses me, I can't really get into it.

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.