简体   繁体   中英

Comparing Two Arrays that don't have standard keys in the object key/value pairs, pushing missing values to first array in javascript

I have seen a lot of questions about this, but I haven't been able to find one that addresses my issue. Let me start with the structure for the array I'm working with (the real array is much longer, this is just an example of the structure):

ServiceArea =  [ 
{'15': 'Montana', checked: true}, 
{'24': 'WA Northwest', checked: true },
{'14': 'WA Southeast', checked: false }  
]

The array I'm attempting to compare it to is structured like this:

data = [
{ ServiceArea: 'Montana', subregionkey: 15 },
{ ServiceArea: 'WA Northwest', subregionkey: 24 },
{ ServiceArea: 'WA Southeast', subregionkey: 14 },
{ ServiceArea: 'WA Southwest', subregionkey: 25 },
]

For the final result, I want any values that are present in the data array but not in the ServiceArea array to be pushed to the ServiceArea array with a "checked" value of true. You can see from the example that the first three objects in data do have corresponding objects in ServiceArea, so in this example, we would be pushing {'25': 'WA Southwest', checked: true} to ServiceArea.

I have attempted several things; one of which was creating a new array with the information in data so that the new array would match the structure of ServiceArea, with the only difference being that checked for all values in the new array is null (instead of true). I then attempted to use.filter(), .some(), and.includes() in various attempts to compare the data. For example:

for (i in data)
ServiceArea.includes(data[i].subregionkey) 
? console.log("true", ServiceArea, data[i].subregionkey)
? console.log("false", ServiceArea, data[i].subregionkey)
}

The console.log s always came back as false, even when I attempted to use data[i].subregionkey.toString() .

I guess my ultimate question is this: do I need to go back to the data architect and tell them that we need to build the ServiceArea array so that it has standard keys, or is there an even moderately simple solution to my issue?

Thank you in advance for any help!!!

Try This:

 let ServiceArea =  [ 
    {'15': 'Montana', checked: true}, 
    {'24': 'WA Northwest', checked: true },
    {'14': 'WA Southeast', checked: false }  
    ];

const data = [
{ ServiceArea: 'Montana', subregionkey: 15 },
{ ServiceArea: 'WA Northwest', subregionkey: 24 },
{ ServiceArea: 'WA Southeast', subregionkey: 14 },
{ ServiceArea: 'WA Southwest', subregionkey: 25 },
];

data.forEach(dataObj => {
    const key = dataObj.subregionkey;
    const value = dataObj.ServiceArea;

    if(!ServiceArea.find(item => item[key] === value)){
        ServiceArea.push({[key]:value, checked:true});
    }
});

console.log(ServiceArea);

You can't use Array#includes to check if an array contains a object with a specific property value. Use Array#forEach with Array#find instead.

 let ServiceArea = [ {'15': 'Montana', checked: true}, {'24': 'WA Northwest', checked: true }, {'14': 'WA Southeast', checked: false } ]; let data = [ { ServiceArea: 'Montana', subregionkey: 15 }, { ServiceArea: 'WA Northwest', subregionkey: 24 }, { ServiceArea: 'WA Southeast', subregionkey: 14 }, { ServiceArea: 'WA Southwest', subregionkey: 25 }, ]; data.forEach(obj=>{ if(.ServiceArea.find(area=>area.hasOwnProperty(obj.subregionkey))){ ServiceArea.push({[obj:subregionkey]. obj,ServiceArea: checked; true}); } }). console;log(ServiceArea);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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