Reputation: 385
i have the following JSON object, which comes from DB in the following format:
{
"total_count": 4,
"items": [
{
"id": "906a1409-b66a-4f8f-b76b-36898828faf2",
"en": "Grade 1B",
"de": "xyz"
},
{
"id": "00ace4db-d29e-4cf4-b25c-f85369db345e",
"en": "Grade 1A",
"de": "xyz"
},
{
"id": "6b4a5578-4f27-4370-9320-a7f57a6c0f54",
"en": "Grade 2A",
"de": "xyz"
},
{
"id": "53b52ee8-dc2e-4c6b-8017-c913d334d5af",
"en": "Grade 2B",
"de": "xyz
}
]
}
but i need it in the following format for the usage of my UI:
{
"total_count": 4,
"items": [
{
"id": "906a1409-b66a-4f8f-b76b-36898828faf2",
"lang": {
"en": "Grade 1B",
"de": "xyz"
}
},
{
"id": "00ace4db-d29e-4cf4-b25c-f85369db345e",
"lang": {
"en": "Grade 1A",
"de": "xyz"
}
},
{
"id": "6b4a5578-4f27-4370-9320-a7f57a6c0f54",
"lang": {
"en": "Grade 2A",
"de": "xyz"
}
},
{
"id": "53b52ee8-dc2e-4c6b-8017-c913d334d5af",
"lang": {
"en": "Grade 2B"
"de": "xyz"
}
]
}
is there an easy javascript way to perform this transformation? maybe map method?
i am trying something like this:
var rows_Original; // this comes from DB
var rows_Result= {}; // new json
rows_Result["total_count"] = rows_Original.length;
rows_Result['items'] = rows_Original;
rows_Result['items'].lang={};
for (var item in rows_Result['items']) {
item[i].lang.en = item.en;
item[i].lang.de = 'item.de;
}
but i am keeping getting that the elements like de, en (Cannot set property 'en' of undefined)
thanks and regards
Upvotes: 1
Views: 128
Reputation: 721
var input = {"total_count": 4,"items": [{"id": "906a1409-b66a-4f8f-b76b-36898828faf2","en": "Grade 1B","de": "xyz"},{"id": "00ace4db-d29e-4cf4-b25c-f85369db345e","en": "Grade 1A","de": "xyz"},{"id": "6b4a5578-4f27-4370-9320-a7f57a6c0f54","en": "Grade 2A","de": "xyz"},{"id": "53b52ee8-dc2e-4c6b-8017-c913d334d5af","en": "Grade 2B","de": "xyz"}]}
function output(inp){
var output = {"total_count" : "","items" : []}
output.total_count = inp.total_count;
var items = inp.items;
items.forEach(item => {
var obj = {"id" : "","lang" : {"en":"","de":""}};
obj.id = item.id;
obj.lang.en = item.en;
obj.lang.de = item.de;
output.items.push(JSON.stringify(obj));
});
return output;
}
console.log(output(input));
Upvotes: 1
Reputation: 35202
You could destructure the id
and get the rest of properties to a lang
variable. Then use Shorthand property names to create a new object with a nested lang
property
const input = {total_count:4,items:[{id:"906a1409-b66a-4f8f-b76b-36898828faf2",en:"Grade 1B",de:"xyz"},{id:"00ace4db-d29e-4cf4-b25c-f85369db345e",en:"Grade 1A",de:"xyz"},{id:"6b4a5578-4f27-4370-9320-a7f57a6c0f54",en:"Grade 2A",de:"xyz"},{id:"53b52ee8-dc2e-4c6b-8017-c913d334d5af",en:"Grade 2B",de:"xyz"}]};
const { total_count, items } = input;
const newItems = items.map(({ id, ...lang }) => ({ id, lang }) ),
output = { total_count, items: newItems };
console.log(output)
If destructuring and rest syntax is not supported, you could loop through the keys of each item like this:
var input = {total_count:4,items:[{id:"906a1409-b66a-4f8f-b76b-36898828faf2",en:"Grade 1B",de:"xyz"},{id:"00ace4db-d29e-4cf4-b25c-f85369db345e",en:"Grade 1A",de:"xyz"},{id:"6b4a5578-4f27-4370-9320-a7f57a6c0f54",en:"Grade 2A",de:"xyz"},{id:"53b52ee8-dc2e-4c6b-8017-c913d334d5af",en:"Grade 2B",de:"xyz"}]};
var output = {
total_count: input.total_count,
items: []
};
for (var item of input.items) {
var newItem = {};
for (var key in item) {
if (key === 'id')
newItem[key] = item[key]
else {
newItem.lang = newItem.lang || {};
newItem.lang[key] = item[key]
}
}
output.items.push(newItem)
}
console.log(output)
Upvotes: 3