Edit
Edit

Reputation: 393

How to merge 2 array using javascript?

I am going to merge 2 array with one array key and other array with key and get only other array value and create final one array in the last. i have write some logic here but i am just put first array key push but other array value can't push in the final array. so anyone know how can do that then please let me here. here i have listed my code with array.

This is my first array=>

var arr = ["fullName","username","biography","externalurl","followerCount","followingCount","medaiCount"];

This is my other array =>

var FinalFilterArray = [ { fullName: 'love',
                    username: 'lo.ve632',
                    biography: '',
                    externalUrl: '',
                    followerCount: 13,
                    followingCount: 129,
                    mediaCount: 0 },
                { fullName: 'abc',
                    username: '@abc',
                    biography: '',
                    externalUrl: '',
                    followerCount: 289,
                    followingCount: 262,
                    mediaCount: 0 }]; 

This is my logic =>

   var ExcelData = [];
    for (var i = 0; i < FinalFilterArray.length; i++) {
        console.log("f" + FinalFilterArray.length)
        if (i == 0) {
            ExcelData[i] = arr
        }
        else {
            var key = [];
            for (var j = 0; j < arr.length; j++) {
                console.log("j " + arr[j]) if(FinalFilterArray[i] == arr[j]){key.push[FinalFilterArray[i].arr[j]]}

            }                       
            ExcelData[i] = [key]
        }
    } 

my Expected o\p =>

 [[ 'fullName',
'username',
'biography',
'externalUrl',
'followerCount',
'followingCount',
'mediaCount' ],
['love','lo.ve632','','','13','129','0'] ,
 ['abc','@abc','','','289','262','0']] 

Upvotes: 0

Views: 135

Answers (1)

Prasanna
Prasanna

Reputation: 4636

finalArr = [ arr , ...FinalFilterArray.map(item => arr.map(key => item[key])) ]

If you want an es5 solution to this, use

finalArr = [arr].concat(FinalFilterArray.map(function(item){
    return arr.map(function(key) {
        return item[key]
    })
})

Upvotes: 2

Related Questions