解析 JSON 并从中构建一个排序数组

Parse a JSON and build a sorted array out of it

我有 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"]
}

有一个唯一的 id 作为键,作为值是品牌、型号和特征的数组。

需要创建一个 JavaScript 函数,当接收到此输入时将 return 作为输出品牌和型号的排序数组,模型是另一个排序的模型数组,唯一 ID,和特点。

以上输入的输出应该是:

[
  {
    "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"]
      }
    ]
  }
]

首先,我解析了输入,然后尝试通过它进行映射,因为它将是一个数组以构建结果。我不知道如何按品牌和型号的字母顺序对其进行排序:

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;
}

您首先必须检查品牌是否已经在数组中,然后(在创建后)将新模型推入数组。

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))

你可以做到这一点。

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))