Is there a javascript library that can transform json like this:
[{},
{title: ""},
{title: "", author: "unknown"},
{isbn: "234234"},
{author: "n/a"}
]
and returns
[{title:"", author: "", isbn: ""},
{title: "", author: "", isbn: ""},
{title: "", author: "unknown", isbn: ""},
{title: "", author: "", isbn: "234234"},
{title: "", author: "n/a", isbn: ""}
]
It was a bit difficult to explain this so I am using an easy to understand example.
update: I do not know the property fields ahead of time. It is dynamic.
this one ?
let origin=
[ { }
, { title: '' }
, { title: '', author: 'unknown' }
, { isbn: '234234' }
, { author: 'n/a' }
]
const modele = origin.reduce((a,c)=>{
for(let p in c) if (!a[p]) { a[p]='' }
return a
},{})
origin.forEach((elm,i)=>origin[i]=Object.assign({}, modele , elm))
// result
origin.forEach(elm=> console.log ( JSON.stringify(elm)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With