0

How do I get the requiredObject data structure from the rows and columns variable? I know I need to use some higher-order array methods like map, etc. I just find the right combination enter image description here

1
  • Posting image for code is bad practice. Is it to avoid some teacher finding his given homework online ? Commented Oct 14, 2021 at 21:06

2 Answers 2

1

This is what I use currently, I am sure there's a better looking solution

let colArray = columns.columns.map(col => col.name)

let transfromedData = []
rows.forEach(row => {
  let temp = {};
  colArray.forEach((key, i) => {
      temp[key] = row[i];
    })
    transfromedData.push(temp)
})
Sign up to request clarification or add additional context in comments.

Comments

0
  const sortedColums = columns.columns.sort((a,b) => a.value - b.value).map(x=>x.name)
  const output = rows.map(x => { const entry = {}; x.map((val, idx) => entry[sortedColums[idx]] = val); return entry})

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂

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.