Set of object array Called Datasource
{"A":[{"accountSoftwareRegUid":1792690,"accountUid":646882,"registrationIdOrOrderItemID":"Not Required For Eval JZ399AAE","registrationType":"RegistrationId",},{"accountSoftwareRegUid":1792691,"accountUid":646882,"registrationIdOrOrderItemID":"Not Required For Eval JZ400AAE","registrationType":"RegistrationId",},{"accountSoftwareRegUid":1792691,"accountUid":646882,"registrationIdOrOrderItemID":"Not Required For Eval JZ400AAE","registrationType":"RegistrationId",},],"B":[{"accountSoftwareRegUid":832633,"accountUid":719261,"registrationIdOrOrderItemID":"1168738","registrationType":"order","passPhrase":"AR0005493ARB",},{"accountSoftwareRegUid":832675,"accountUid":719261,"registrationIdOrOrderItemID":"1168739","registrationType":"order","passPhrase":"AR0005493ARB",},],"C":[{"accountSoftwareRegUid":651923,"accountUid":646882,"registrationIdOrOrderItemID":"1384120","registrationType":"order","passPhrase":null,"organisationName":"flyasiana",},{"accountSoftwareRegUid":637694,"accountUid":646882,"registrationIdOrOrderItemID":"1116033","registrationType":"order","passPhrase":null,"organisationName":"korea Correctional Service",},],}
and I have another set of array-like
accountSoftwareRegUid[1792690,832633]
I Need output looks like
{"A":[{"accountSoftwareRegUid":1792690,"accountUid":646882,"registrationIdOrOrderItemID":"Not Required For Eval JZ399AAE","registrationType":"RegistrationId",},],"B":[{"accountSoftwareRegUid":832633,"accountUid":719261,"registrationIdOrOrderItemID":"1168738","registrationType":"order","passPhrase":"AR0005493ARB",},],}
CodePudding user response:
You want to filter?
const r = {
model: {
// This says keep the elements of datasource that have a Uid that appears in the other set
A: datasource.model.A.filter(data => accountSoftwareRegUid.some(id => data.accountSoftwareRegUid === id)),
},
}
CodePudding user response:
const dataSource = {
"A": [{
"accountSoftwareRegUid": 1792690,
"accountUid": 646882,
"registrationIdOrOrderItemID": "Not Required For Eval JZ399AAE",
"registrationType": "RegistrationId",
}, {
"accountSoftwareRegUid": 1792691,
"accountUid": 646882,
"registrationIdOrOrderItemID": "Not Required For Eval JZ400AAE",
"registrationType": "RegistrationId",
}, {
"accountSoftwareRegUid": 1792691,
"accountUid": 646882,
"registrationIdOrOrderItemID": "Not Required For Eval JZ400AAE",
"registrationType": "RegistrationId",
}, ],
"B": [{
"accountSoftwareRegUid": 832633,
"accountUid": 719261,
"registrationIdOrOrderItemID": "1168738",
"registrationType": "order",
"passPhrase": "AR0005493ARB",
}, {
"accountSoftwareRegUid": 832675,
"accountUid": 719261,
"registrationIdOrOrderItemID": "1168739",
"registrationType": "order",
"passPhrase": "AR0005493ARB",
}, ],
"C": [{
"accountSoftwareRegUid": 651923,
"accountUid": 646882,
"registrationIdOrOrderItemID": "1384120",
"registrationType": "order",
"passPhrase": null,
"organisationName": "flyasiana",
}, {
"accountSoftwareRegUid": 637694,
"accountUid": 646882,
"registrationIdOrOrderItemID": "1116033",
"registrationType": "order",
"passPhrase": null,
"organisationName": "korea Correctional Service",
}, ],
};
const accountSoftwareRegUid = [1792690, 832633];
const result = {};
function getRecords(dataSource, accountSoftwareRegUid){
Object.keys(dataSource).forEach((key) => {
dataSource[key].forEach((record) => {
if(accountSoftwareRegUid.includes(record.accountSoftwareRegUid)){
if(result[key]){ // if result with the key exists the push it
result[key].push(record);
}else{ // else create a new key
result[key] = [record];
}
}
})
})
return result;
}
console.log(getRecords(dataSource, accountSoftwareRegUid));