-2

There are two arrays:

a1 = [0, 2, 4, 6, 8];
a2 = [1, 3, 5, 7, 9];

How I get this:

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

I wanna to use RxJs or else.

If the value isn‘t a number, like an Object.

 a1 = [A, C, E, G, I]; 
 a1 = [{'name': 'Lucy'}, {'name': 'Lily'}, {'name': 'Jerry'}, {'name': 'Tom'}, {'name': 'Smith'}]

 a2 = [B, D, F, H, J]; 
 a2 = [{'name': 'Jack'}, {'name': 'John'}, {'name': 'Anan'}, {'name': 'Bob'}, {'name': 'Dav'}]

 a = [A, B, C, D, E, F, G, H, I, J];
9
  • a1.concat(a2) Commented Mar 31, 2018 at 13:17
  • 1
    a1.concat(a2).sort() actually Commented Mar 31, 2018 at 13:17
  • a1.concat(a2).sort(); Commented Mar 31, 2018 at 13:18
  • WHile I was typing... ;) Commented Mar 31, 2018 at 13:18
  • 1
    Why you vote -1? Commented Mar 31, 2018 at 13:28

3 Answers 3

1
[...a1,...a2].sort((a,b) => a-b)

That's ES6 syntax.

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

1 Comment

How should that work with objects ?!?
0

With concat() and sort()

a1 = [0, 2, 4, 6, 8];
a2 = [1, 3, 5, 7, 9];

a = a1.concat(a2).sort();

console.log(a)

3 Comments

How should that work with objects?!
He edited its question. Never talked about objects before
I just used number for a example....
0
  const result = [];

  for(var i = 0; i < arr1.length && i < arr2.length; i++)
    result.push(arr1[i], arr2[i]);

  result push(...arr1.slice(i), ...arr2.slice(i));

That merges two arrays (arr1, arr2) by taking one each.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.