The script below currently outputs:
{
"title1": {
"0": "undefined"
},
"title2": "& three",
"title3": "& four"
}
My question is what is required in the if statement to successfully loop through the properties of the [key][innerKey] and pass them through the encode function and return them to 'out'?
I have been trying to solve it using a for loop within the first part of the if statement without success. I figure it needs to follow a logic along the lines of:
- if arr[key] is an object then
- output the [key]:
- then loop through all properties of the [key]
- and pass them through the function encode
- and return them to 'out'
thanks.
var arr = {
"title1": {
"nested1": "one",
"nested2": "& two"
},
"title2": "& three ",
"title3": "& four"
};
var encodedValues = Object.keys(arr).reduce(function(out,key,innerKey) {
if (typeof arr[key] == "object") {
return Object.assign(out, {[key]: {[innerKey]: encode(arr[key][innerKey])}})
} else {
return Object.assign(out, {[key]: encode(arr[key])})
}
}, {});
console.log(encodedValues)
Aucun commentaire:
Enregistrer un commentaire