1

I have an object like this :

[{name:'abcd', data:[1,2,3,4,5,6]},{name:'cdef', data:[7,8,9,10,11,12]}]

and I would like to format it like this :

[{abcd:1, cdef:7},{abcd:2, cdef:8}, {abcd:3, cdef:9}, ... ]

help please !

1
  • What are your attempts so far? Commented Dec 15, 2017 at 11:55

1 Answer 1

1

You can use reduce() and forEach() methods.

var data = [{name:'abcd', data:[1,2,3,4,5,6]},{name:'cdef', data:[7,8,9,10,11,12]}]

var result = data.reduce(function(r, {name, data}) {
  data.forEach((e, i) => {
    if(!r[i]) r[i] = {[name]: e};
    else r[i][name] = e;
  })
  return r;
}, [])

console.log(result)

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

Comments

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.