简体   繁体   English

如何使用JavaScript合并2个数组?

[英]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. 我将合并2个数组和一个数组键,以及另一个数组和键,并仅获取其他数组值,并在最后一个数组中创建最后一个数组。 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 => 我的预期o \\ p =>

 [[ 'fullName',
'username',
'biography',
'externalUrl',
'followerCount',
'followingCount',
'mediaCount' ],
['love','lo.ve632','','','13','129','0'] ,
 ['abc','@abc','','','289','262','0']] 
finalArr = [ arr , ...FinalFilterArray.map(item => arr.map(key => item[key])) ]

If you want an es5 solution to this, use 如果您想要es5解决方案,请使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM