0

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']] 
1
  • I think using concat would be a good option to merge them. What you can do is apply logic to fetch values and use varname.concat(); to merge the details Commented Nov 6, 2017 at 9:02

1 Answer 1

2
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]
    })
})
Sign up to request clarification or add additional context in comments.

6 Comments

That's a ES6 solution to be precise . The ... operator does not exists in ES5
Added an ES5 solution
i am go for generate excel file and set this array in excel file so using your solution i am not getting propare array for set in excel file.
i mean i am finalarray used and pass for generate excel file so i am wrong o/p getting in file so can you please help me
I am really sorry, I could not understand what you were trying to say. But the finalArray in the solution is the same array that you expect your ExcelData variable to have.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.