Concat used to sort and group the most specific nested processing arrays, sorted according to the conditions and

Recent project encountered such a demand, more needs to be based on an array of arrays of field ordering, and then to the page rendering.

The initial array like this

    var ARR = [ 
            { 
                of arr1: [ 
                    {    
                        name: 'I'm a commodity. 6' , 
                        Sort: . 6 
                    }, 
                    {    
                        name: 'I'm a commodity. 3' , 
                        Sort: . 3 
                    } 
                ], 
                arr2 is: [ 
                    {    
                        name: 'I 5 of 5 ' , 
                        Sort: 2 
                    }, 
                    {   
                        name: 'I'm a commodity. 1' , 
                        Sort: . 1 
                    } 
                ] 
            }, 
            { 
                of arr1: [ 
                    {    
                        name: 'I'm a commodity. 5' , 
                        Sort: . 5 
                    } 
                ], 
                arr2 is: [ 
                    {    
                        name: 'I product. 4' , 
                        Sort : 4 
                    } 
                ] 
            } 
        ]

It should be sorted according to sort fields, from 1 to 6

demand:

  1. The source array consisting of an array weight

        var o = []
        arr.forEach((item,index) => {
            o = o.concat(item.arr1,item.arr2)
            o.sort(function (a, b) {
                return a.sort - b.sort
            })
        })

  The output is:

  

 

   2. Generate a new array in each data array arr

        arr.forEach((item,index) => {
            item.arr3 = [].concat(item.arr1,item.arr2)
            item.arr3.sort(function (a, b) {
                return a.sort - b.sort
            })
        })

  The output is:

 

 

There is a place worth noting, concat more arrays can be combined, but do not make changes to the previous array, it will return a new array

Guess you like

Origin www.cnblogs.com/qiuchuanji/p/11445409.html