0

So suppose I have a few different Arrays as follows:

var first = [{key1: x},{key1:y},{key1:z},{key1:a},{key1:b},{key1:v}, ...];
var second = [{key2: anything},{key2:...},{key2:...},{key2:...},{key2:...},{key2:...}, ...];
var third = [{key3: value},{key3:...},{key3:...},{key3:...},{key3:...},{key3:...}, ...];
var fourth = [{key4:another value},{key4:...},{key4:...},{key4:...},{key4:...},{key4:...}];
var fifth = [{key5: and another one},{key5:...},{key5:...},{key5:...},{key5:...},{key5:...}];
.
.
.
and so on...

now I would like to merge them into one array where my new objects contain one of each of the other arrays like so:

var newBigArray = [{key1: x, key2: anything, key3: value, key4: another value, key5: and another one (here in this object the first of each of the original array's objects merged into one},{here in the second object the second of each of the original array's objects...},{here the third...},{fourth},{fifth},{...},...];

I hope you get the idea.

I have tried the .push() method, the Object.assign(), and some variations of for-loops but I can't find the solution.

How would one go about this, any ideas?

6
  • [first, second, third, fourth, fifth].map(array => arrray[0]); Commented Jun 1, 2021 at 19:54
  • What happens when one array is shorter than the others? Or longer? Commented Jun 1, 2021 at 20:02
  • Just do a normal indexed for loop and use How to concatenate properties from multiple JavaScript objects to combine one element from each array at each index. Commented Jun 1, 2021 at 20:07
  • Does this answer your question? Merge multiple objects inside the same array into one object Commented Jun 1, 2021 at 20:10
  • @HereticMonkey Well not quite, the keys are the same in the original Arrays and as far as I unterstood, that is why the values get overriden when I try the conventional methods... Commented Jun 1, 2021 at 20:35

2 Answers 2

3

You could collect all objects in an array and assign all properties of the same index to a single object.

const
    first = [{ key1: 'a' }, { key1: 'b' }, { key1: 'c' }, { key1: 'd' }, { key1: 'e' }, { key1: 'f' }],
    second = [{ key2: 1 } , { key2: 2 }, { key2: 3 }, { key2: 4 }, { key2: 5 }, { key2: 6 }],
    third = [{ key3: 'z' }, { key3: 'y' }, { key3: 'x' }, { key3: 'w' }, { key3: 'v' }, { key3: 'u' }],
    result = [first, second, third]
        .reduce((r, a) => a.map((o, i) => Object.assign(r[i] || {}, o)), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

Can you make this executable so you can show that it produces the desired result?
@NinaScholz, hey, so it does work quite well, but the last object only contains the last key, could you think of any reason why this is? I used your line and just substituted [first, second, third] with my arraynames
maybe the lengths are not equal.
-2

You can use the spread operator:

let first = [{x: 1, y:2}]
let second = [{x: 2, y:2}]
let third = [{x: 3, y:2}]
let fourth = [{x: 4, y:2}]
let fifth = [{x: 5, y:2}]

let finalArray = [
  ...first,
  ...second,
  ...third,
  ...fourth,
  ...fifth
]

console.log(finalArray)

2 Comments

That doesn't look like the desired result.
no the point is, that in one array, all the keys are the same and I need to spread them evenly within new objects in a new single array

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.