-2

I want to update the data which is array based on key present in another array and if key is not present output the data with value 0 along with corresponding key

let data = [{
    ORIGEN: "WB716594",
    "Gestor Ericsson OSS": 1
}, {
    ORIGEN: "MM-LORC-AGUI-L3-11_760525",
    "MM-LORC-AGUI-L3-11_760525": 1
}];

let keyValue = ["Gestor Ericsson OSS",
    "MM-LORC-AGUI-L3-11_760525",
    "Gestor HUA U2KVIA",
    "5620SAM"
]

Here "Gestor HUA U2KVIA" and "5620SAM" is not present in the data and hence it should be append as below

Output = [{ORIGEN: "WB716594", Gestor Ericsson OSS: 1, "MM-LORC-AGUI-L3-11_760525":0,Gestor HUA U2KVIA:0,5620SAM:0},
 {ORIGEN: "MM-LORC-AGUI-L3-11_760525", MM-LORC-AGUI-L3-11_760525: 1,Gestor Ericsson OSS: 0,Gestor HUA U2KVIA:0,5620SAM:0}
 ]
adiga
  • 34,372
  • 9
  • 61
  • 83
Gautham Shetty
  • 361
  • 1
  • 5
  • 13
  • 2
    Can you properly describe your problem? It is not comprehensible. – fjc Apr 25 '19 at 11:05
  • That JSON/Notation Object is not valid. Also what is count data? – Adrian Apr 25 '19 at 11:07
  • In data array i am not getting all the key, I want to modify based on keyValue, Whichever key is not present i want to update the count to 0 as shown in Output.The first value ORIGEN in the object is groupby value which will be present in all the object in the array – Gautham Shetty Apr 25 '19 at 11:07
  • Please [edit] the question and include a valid [mcve] - No point explaining yourself in the comments – Alon Eitan Apr 25 '19 at 11:13
  • Hi, this is already provided in description – Gautham Shetty Apr 25 '19 at 11:14

1 Answers1

1

You could create an object with keyValue array items as key and 0 as value. Then loop through the data array using map and use Object.assign to get the merged object for each item:

const data = [{ORIGEN:"WB716594","Gestor Ericsson OSS":1},{ORIGEN:"MM-LORC-AGUI-L3-11_760525","MM-LORC-AGUI-L3-11_760525":1}],
     keyValue = ["Gestor Ericsson OSS","MM-LORC-AGUI-L3-11_760525","Gestor HUA U2KVIA","5620SAM"]

const defaultObj = Object.assign({}, ...keyValue.map(key => ({ [key]: 0 })));
const output = data.map(obj => Object.assign({}, defaultObj, obj));

console.log(output)
adiga
  • 34,372
  • 9
  • 61
  • 83