I want to combine array [0] and [1] into an integer array in javascript

I have an array called id_list. It has an index of 0.1

id_list[0]= 200, 201, 202, 203 id_list[1]= 300, 301, 302, 303 

I want to combine index[0] and index[1] into an integer array.
I used .join() , but it concatenates the entire element, including , into an array.

 var arr = id_list[0].join(id_list[1]); console.log(arr[0]); // result => 2 console.log(arr[1]); // result => 0 console.log(arr[3]); // result => , 

I just want id_list= [200, 201, 202, 203, 300, 301, 302, 303] .
I do not want value.

Please help me how to merge them.

+4
source share
3 answers

Your example and the way to get individual characters in your results show that your values ​​are actually strings, for example:

 var id_list = []; id_list[0] = '200, 201, 202, 203'; id_list[1] = '300, 301, 302, 303'; 

If this is the case, your first step should be converted to real arrays. You can do this simply by dividing by,, but a regular expression that removes spaces will also give you a cleaner result. / *, */ will look for a comma with optional spaces before or after it:

 id_list[0] = id_list[0].split(/ *, */); // now id_list[0] is an array of 4 items: ['200', '201', '202', '203'] id_list[1] = id_list[1].split(/ *, */); // now id_list[1] is an array of 4 items: ['300', '301', '302', '303'] 

Then you can use concat() to combine arrays into one array:

 var arr = id_list[0].concat(id_list[1]); 

I suspect this is more like the result you expect:

 console.log(arr[0]); // shows 200 console.log(arr[1]); // shows 201 console.log(arr[3]); // shows 203 console.log(arr[5]); // shows 301 
+3
source

Try using Array.prototype.concat

 var newArr = id_list[0].concat(id_list[1]); 

Fiddle

In your case, you can simply do:

 id_list = id_list[0].concat(id_list[1]); 

Read about it here.

+3
source

Use concat method

 id_list[0].concat(id_list[1]) 
+3
source

Source: https://habr.com/ru/post/1487410/


All Articles