I have an array like [a, b, c, d] and I want to split it into 2 arrays like [a, b] and [c, d] and then merge it to have final result like [[a, b],[c, d]]. Is it possible to do without for loop?
            Asked
            
        
        
            Active
            
        
            Viewed 1,752 times
        
    0
            
            
        - 
                    1Possible duplicate of [Split array into two arrays](https://stackoverflow.com/questions/9933662/split-array-into-two-arrays) – Mohammad Usman Jun 20 '19 at 09:26
- 
                    1Why you want to do without loop ? – Code Maniac Jun 20 '19 at 09:26
- 
                    1Possible duplicate of [Split array into chunks](https://stackoverflow.com/questions/8495687/split-array-into-chunks) – adiga Jun 20 '19 at 09:27
- 
                    1I guess it's not really possible without loops unless you statically assign indexes to access each. Just use loop for it. – Martin Lloyd Jose Jun 20 '19 at 09:29
- 
                    You gave an example, but do you need code for a more general case? If yes, how would you exactly specify that general case? Alternatively, share the code with the loop(s) and then we will try to rewrite it without loops. – www.admiraalit.nl Jun 20 '19 at 09:32
5 Answers
5
            
            
        You can use slice and push method like this
var arr = ['a', 'b', 'c', 'd'];
let index = 2;
let result = [];
result.push(arr.slice(0, index));
result.push(arr.slice(index))
console.log(result); 
    
    
        Hien Nguyen
        
- 24,551
- 7
- 52
- 62
- 
                    Thanks for your answer. Everything is fine, but found another way how to solve it. – Donnie Jun 21 '19 at 06:48
0
            
            
        yes without loop you can do this But you should know at what index you have to split the array.
var arr = ['a', 'b', 'c', 'd'];  
var indexToSplit = arr.indexOf('c');
var first = arr.slice(0, indexToSplit);
var second = arr.slice(indexToSplit + 1);
var final = [first, second]
    console.log(final); 
    
    
        anurag sharma
        
- 61
- 1
- 6
- 
                    Please also specify the second step: merging the two pieces together, as requested. – www.admiraalit.nl Jun 20 '19 at 09:49
0
            
            
        let arr = [0, 1, 9, 10, 8];
    let arr2 = arr.slice(1,3);
    let resultArr = [];
    if(arr2[1] > 1){
    resultArr.push(99); 
    }
else{
    resultArr.push(100); 
}
    console.log(resultArr)
 
    
    
        Arvind Yadav
        
- 13
- 3
- 
                    splice arr array and get new array arr2 and based on condition we push element on result array – Arvind Yadav Jun 20 '19 at 10:14
0
            
            
        You might like something like this:
a = 8;
b = { some: 'say'};
c = 'life';
d = true;
let o = {
    'a': [a, b, c, d],
    'a1': [],
    'a2': []
}
nSwitchBefore = 2;
o.a.forEach(function(item, i) {
   i < nSwitchBefore ? this.a1.push(item) : this.a2.push(item) ;
}.bind(o));
console.log(o);
Within the function block there is room for extra handling your array items. Like filtering or special treatment of certain types, using all conditions you want.
 
    
    
        Duck
        
- 76
- 4
0
            
            
        I found another solution for this issue, without writing loops Lodash Chunk does this logic
_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]
 
    
    
        Donnie
        
- 373
- 2
- 7
- 28