Comparing the properties of objects of two arrays and storing it in a new array

时间:2019-01-07 12:54:36

标签: javascript

I would like to compare a property of an object of two arrays of different lengths. If my condition is true (gender check) and then if that property matches then I would like to combine the properties of that object from both arrays and store it in a different array.

For example:

// array 1
var array1 = [{name: 'Jan', age: 19, category: {gender: 'Male'}}, {name: 'Suzy', age: 29, category: {gender: 'Female'}}, {name: 'Peter', age: 39, category: {gender: 'Male'}}, {name: 'Bart', age: 49, category: {gender: 'Male'}}, {name: 'John', age: 59, category: {gender: 'Male'}}];
// array 2
var array2 = [{name:'Kean', job: 'Technician'},{name:'Nick', job:'Mathematics'},{name: 'Jan', job: 'Tester'}, {name: 'Suzy', job:'Developer'}, {name: 'Peter', job: 'Scrum master'}]

Expected result:

var resultMale = [{name: 'Jan', age: 19,job: 'Tester'}, {name: 'Peter', age: 39, job: 'Scrum master'}];
var resultFemale = [{name: 'Suzy', age: 29, job:'Developer'}];

Below is my attempt just to show that I have been putting all my effort to find a solution myself. I have changed all the functions and variable names this.

xxxxxxxx.getContractsForRules().then(res => {
        // res.xxxxxx.forEach(function (obj) {
        //     if(obj.contract.product.xxxxxxx=== 'xxxxxxx') {
        //         console.log(this.xxxxxx.xx);
        //         for(let i=0; i < this.xxxxxxx.length; i++) {
        //             if(obj.contract.accountNumber === this.xxxxxxxx[i].ibanNumber) {
        //                 this.currentAccount = {
        //                     accountNumber: res.xxxxx[i].contract.accountNumber,
        //                     accountName: res.xxxxx[i].contract.customer.xxxxxx
        //                 };
        //                 this.xxxxxxx.push(this.xxxxxx);
        //             }
        //         };
        //     }
        // });

        this.result = res.contractList.filter(item => this.allCurrentAccounts.);

            if(res.xxxx[i].contract.xxxxx=== this.xxxxx[i].ibanNumber) {
                this.savingAccount = {
                    accountNumber: xxxx.xxxx[i].contract.accountNumber,
                    accountName: res.xxxxx[i].contract.customer.xxxxx
                };
                this.xxxxx.push(this.xxxxx);
            }

    });
    this.test();
}

1 个答案:

答案 0 :(得分:1)

What you finally need is an Intersection of both the arrays. So, you could do the following -

var array1 = [{ name: 'Jan', age: 19, category: { gender: 'Male' } }, { name: 'Suzy', age: 29, category: { gender: 'Female' } }, { name: 'Peter', age: 39, category: { gender: 'Male' } }, { name: 'Bart', age: 49, category: { gender: 'Male' } }, { name: 'John', age: 59, category: { gender: 'Male' } }];

var array2 = [{ name: 'Kean', job: 'Technician' }, { name: 'Nick', job: 'Mathematics' }, { name: 'Jan', job: 'Tester' }, { name: 'Suzy', job: 'Developer' }, { name: 'Peter', job: 'Scrum master' }];

// Empty arrays to contain final intersection array for both male & females
var resultMale = [], resultFemale = [];

/* now looping over both arrays to traverse all the elements from them */

// iterating over first array
array1.forEach(x => {
// iterating over second array
array2.forEach(y => {
    // collect the objects only if the name attribute matches in both
    if (x.name == y.name) {
        // push into male array if gender is male else push into female array
        if (x.category && x.category['gender'] == 'Male') {
            resultMale.push({
                name: x.name,
                age: x.age,
                job: y.job
            });
        } else if (x.category && x.category['gender'] == 'Female') {
            resultFemale.push({
                name: x.name,
                age: x.age,
                job: y.job
            });
        }
    }
});
});

console.log(resultMale);
console.log(resultFemale);

Note - this can be optimized to reduce the time complexity.