I'm looking for an elegant way to do the following:
I have a JS object:
var sObject = {};
sObject[Col1] = "Value1";
sObject[Col2] = "Value2";
sObject[Col2] = "Value3";
I also have an array
var arrayCols=[];
arrayCols.push("Col2");
What I am trying to do is remove all the key/values from sObject
where Key != arrayCols key
Once the needful is done, my sObject should only contain Col2:Value2 as Col2
is the only key in the arrayCols
I'm trying something along the lines of:
var sObject = {};
sObject['Col1'] = "Value1";
sObject['Col2'] = "Value2";
sObject['Col2'] = "Value3";
var arrayCols = [];
arrayCols.push("Col2");
let newObject = {};
for (const [key, value] of Object.entries(sObject)) {
if (key in arrayCols) {
newObject[key] = value;
}
}
console.log(newObject);