If I have available keys like:
[
"cars[].model",
"cars[].make",
"cars[].year",
"toys.color",
"toys.type[].brand",
"toys.type[].price",
"id",
"books[].publisher[].authors[]"
]
where cars[].model represents that cars is an array of objects with model as one of the keys.
And if the input is:
{
"cars": [
{
"make": "Audi"
},
{
"model": "A8",
"year": "2007"
}
],
"id": "xyz",
"extra": "test",
"toys": {
"color": "Black",
"type": [
{
"price": "$100"
}
]
}
}
How do I create an object based on these available keys and the input object?
Targeted output:
{
"cars": [
{
"model": "",
"make": "Audi",
"year": ""
},
{
"model": "A8",
"make": "",
"year": "2007"
}
],
"toys": {
"color": "Black",
"type": [
{
"brand": "",
"price": "$100"
}
]
},
"id": "xyz",
"books": [
{
"publisher": [
{
"authors": []
}
]
}
],
"extra": "test"
}
If the available keys doesn't exist, the value is empty.
I have tried flattening/unflattening the nested object, but in vain.
Any help would be appreciated.
CodePudding user response:
This is a job for recursion. I defined a function, addKeysRec
, that would add in the missing elements in the input
array:
function addKeysRec(input, keyArray, recursiveCalls=0) {
if (recursiveCalls == keyArray.length) return
const k = keyArray[recursiveCalls]
if (k == "[]") {
if (!input.length && recursiveCalls 1 < keyArray.length) input.push({})
input.forEach(s => addKeysRec(s, keyArray, recursiveCalls 1))
} else {
if (!input.hasOwnProperty(k)) {
if (keyArray[recursiveCalls 1] == null) input[k] = ''
else if (keyArray[recursiveCalls 1] == '[]') input[k] = []
else input[k] = {}
}
addKeysRec(input[k], keyArray, recursiveCalls 1)
}
}
The function in action:
const keys = [
"cars[].model",
"cars[].make",
"cars[].year",
"toys.color",
"toys.type[].brand",
"toys.type[].price",
"id",
"books[].publisher[].authors[]"
]
const input = {
"cars": [
{
"make": "Audi"
},
{
"model": "A8",
"year": "2007"
}
],
"id": "xyz",
"extra": "test",
"toys": {
"color": "Black",
"type": [
{
"price": "$100"
}
]
}
}
function addKeysRec(input, keyArray, recursiveCalls=0) {
if (recursiveCalls == keyArray.length) return
const k = keyArray[recursiveCalls]
if (k == "[]") {
if (!input.length && recursiveCalls 1 < keyArray.length) input.push({})
input.forEach(s => addKeysRec(s, keyArray, recursiveCalls 1))
} else {
if (!input.hasOwnProperty(k)) {
if (keyArray[recursiveCalls 1] == null) input[k] = ''
else if (keyArray[recursiveCalls 1] == '[]') input[k] = []
else input[k] = {}
}
addKeysRec(input[k], keyArray, recursiveCalls 1)
}
}
keys.forEach(key => {
keyArray = key.replace(/\[\]/g, '.[]').split(".")
addKeysRec(input, keyArray)
})
console.log(input)
Output:
{
"cars": [
{
"model": "",
"make": "Audi",
"year": ""
},
{
"model": "A8",
"make": "",
"year": "2007"
}
],
"toys": {
"color": "Black",
"type": [
{
"brand": "",
"price": "$100"
}
]
},
"id": "xyz",
"books": [
{
"publisher": [
{
"authors": []
}
]
}
],
"extra": "test"
}
The line:
key = key.replace(/\[\]/g, '.[]').split(".")
is to convert each key to an array, from, for example, the format "cars[].model"
to ["cars", "[]", "model"]
.