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 !

asked Dec 15, 2017 at 11:51
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)

answered Dec 15, 2017 at 12:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.