I have JSON with the following structure as an input:
{
"143334": ["VW", "Golf", "Diesel or Gas", "Affordable", "Blue, red or white"],
"344389": ["VW", "Passat", "Diesel or Gas", "More expensive"],
"484612": ["Audi", "A8", "Diesel", "Premium"],
"234234": ["Renault", "Megane", "not too much info"]
}
There is a unique id as key, and as value is an array of brand, model, and characteristics.
What is needed is to create a JavaScript function that when receives this input will return as output a sorted array of brands and models, models being another sorted array of model, unique id, and characteristics.
The output for the above input should be:
[
{
"brand": "Audi",
"models": [
{
"model": "A8",
"id": "484612",
"characteristics": ["Diesel", "Premium"]
}
]
},
{
"brand": "Renault",
"models": [
{
"model": "Megane",
"id": "234234",
"characteristics": ["not too much info"]
}
]
},
{
"brand": "VW",
"models": [
{
"model": "Golf",
"id": "143334",
"characteristics": ["Diesel or Gas", "Affordable", "Blue, red or white"]
},
{
"model": "Passat",
"id": "344389",
"characteristics": ["Diesel or Gas", "More expensive"]
}
]
}
]
Firstly, I've parsed the input, then tried to map through it as it would be an array in order to build the result. I have no idea how to sort it alphabetically by brand and by models:
function doMagic(input) {
const result = [];
const parsedInput = JSON.parse(input);
for (const brand in parsedInput) {
result.push({"brand": brand[0], "models": brand[1], "id": brand});
}
return result;
}
CodePudding user response:
You first have to check, if the brand is already in the array, and (after it's created) push the new model into the array.
let json = '{"143334": ["VW", "Golf", "Diesel or Gas", "Affordable", "Blue, red or white"],"344389": ["VW", "Passat", "Diesel or Gas", "More expensive"],"484612": ["Audi", "A8", "Diesel", "Premium"],"234234": ["Renault", "Megane", "not too much info"]}';
function doMagic(json) {
let res = [];
let arr = JSON.parse(json)
Object.keys(arr).forEach(k => {
let tempIndex = res.findIndex(r => r.brand === arr[k][0])
if(-1 === tempIndex) { // if the brand isn't in the array yet
tempIndex = res.push({
brand:arr[k][0],
models:[]
})-1
}
res[tempIndex].models.push({
"model": arr[k][1],
"id": k,
"characteristics": arr[k].slice(2)
})
})
res.sort((a,b) => a.brand.localeCompare(b.brand))
res.forEach(e => e.models.sort((a,b) => a.model.localeCompare(b.model)))
return res
}
console.log(doMagic(json))
CodePudding user response:
You can do this.
let jsonObj = '{"143334": ["VW", "Golf", "Diesel or Gas", "Affordable", "Blue, red or white"],"344389": ["VW", "Passat", "Diesel or Gas", "More expensive"],"484612": ["Audi", "A8", "Diesel", "Premium"],"234234": ["Renault", "Megane", "not too much info"]}';
function doMagic(input) {
const parsedInput = JSON.parse(input);
var ids = Object.keys(parsedInput);
var mappingObj = {};
for(let id of ids){
if(mappingObj[parsedInput[id][0]]){
mappingObj[parsedInput[id][0]].push({
model:parsedInput[id][1],
id:id,
characteristics:parsedInput[id].slice(2)
})
}else{
mappingObj[parsedInput[id][0]]= [
{
model:parsedInput[id][1],
id:id,
characteristics:parsedInput[id].slice(2)
}
]
}
}
var brands = Object.keys(mappingObj)
brands.sort(function(a,b){
if(a>b){return 1;}
else if(a<b){return -1;}
else {return 0;}
})
var finalObj = [];
for(var i=0;i<brands.length;i ){
mappingObj[brands[i]].sort(function(a,b){
if(a.model>b.model){return 1;}
else if(a.model<b.model){return -1;}
else {return 0;}
})
finalObj.push({
brand:brands[i],
models:mappingObj[brands[i]]
})
}
return finalObj;
}
console.log(doMagic(jsonObj))
