简体   繁体   English

如何使用 Javascript 更改我的 json 对象的结构

[英]How to change the structure of my json object using Javascript

I have a json object which stores all the vehicle models with their brand. 

[{
"brand":"Audi","model":"A4"
},
{
"brand":"Audi","model":"A6"
},
{
"brand":"BMW","model":"Z4"
},
{
"brand":"Audi","model":"R8"
},
{
"brand":"Volvo","model":"v40"
},
{
"brand":"BMW","model":"5 Series"
}]

But the problem is , If I have 3 models for Audi, then the list is repeating the brand again and again thus filling my list with duplicate values.但问题是,如果我有 3 款奥迪车型,那么列表会一次又一次地重复这个品牌,从而用重复的值填充我的列表。 So I need to make a json object something like this: I just want to make a json object as follows using javascript or angularjs:所以我需要制作一个这样的 json 对象:我只想使用 javascript 或 angularjs 制作一个 json 对象,如下所示:

 [ {"brand":"Audi","models":["A4","A6","R8"]},
    {"brand":"BMW","models":["Z4","5 Series"]},
    {"brand":"Volvo","models":["v40"]}]

Given your input:鉴于您的意见:

var input = [{
    "brand": "Audi",
    "model": "A4"
}, {
    "brand": "Audi",
    "model": "A6"
}, {
    "brand": "BMW",
    "model": "Z4"
}, {
    "brand": "Audi",
    "model": "R8"
}, {
    "brand": "Volvo",
    "model": "v40"
}, {
    "brand": "BMW",
    "model": "5 Series"
}];

you can achieve your desired result using Array.reduce -您可以使用Array.reduce实现您想要的结果 -

var output = input.reduce(function(work, datum) {
    var n = work.brands.indexOf(datum.brand);
    if (n < 0) {
        n = work.brands.push(datum.brand) - 1;
        work.result.push({brand: datum.brand, models: []});
    }
    work.result[n].models.push(datum.model);
    return work;
}, {brands:[], result:[]}).result;

you can make the JSON easily using both angular js or javascript.您可以使用 angular js 或 javascript 轻松制作 JSON。 You can use following method in angular js .您可以在 angular js 中使用以下方法。 In javascript just use same type of loop like 'for'.在 javascript 中,只需使用相同类型的循环,如“for”。

var model = {};
var brandList = [];
var returnObject = [];

var data = [
  {
    "brand": "Audi", "model": "A4"
  },
  {
    "brand": "Audi", "model": "A6"
  },
  {
    "brand": "BMW", "model": "Z4"
  },
  {
    "brand": "Audi", "model": "R8"
  },
  {
    "brand": "Volvo", "model": "v40"
  },
  {
    "brand": "BMW", "model": "5 Series"
  }
]

angular.forEach(data, function (dat, index) {
  if (brandList.indexOf(dat.brand) == -1) {
    brandList.push(dat.brand);
    model[dat.brand] = [];
    model[dat.brand].push(dat.model);
  } else {
    model[dat.brand].push(dat.model);
  }
});

angular.forEach(brandList, function (val, index) {
  var obj = {};
  obj.brand = val;
  obj.models = model[val];
  returnObject.push(obj);
});

Now returnObject will hold your desired JSON object.现在 returnObject 将保存您想要的 JSON 对象。

var input = [{
    "brand": "Audi",
    "model": "A4"
}, {
    "brand": "Audi",
    "model": "A6"
}, {
    "brand": "BMW",
    "model": "Z4"
}, {
    "brand": "Audi",
    "model": "R8"
}, {
    "brand": "Volvo",
    "model": "v40"
}, {
    "brand": "BMW",
    "model": "5 Series"
}];

// collect all brands models in a map
var brands_models = {};
for(var i=0; i < input.length; i++) {
 var dat = input[i];
 brands_models[dat.brand] = brands_models[dat.brand] || [];
 brands_models[dat.brand].push(dat.model);
}

// make array from map
var output = [];
foreach(var brand in brands_models) {
 output.push({
    "brand" : brand,
    "models": brands_models[brand]
  });
}

A proposal with only Array.prototype.forEach and Array.prototype.some .只有Array.prototype.forEachArray.prototype.some提案。

 var data = [{ "brand": "Audi", "model": "A4" }, { "brand": "Audi", "model": "A6" }, { "brand": "BMW", "model": "Z4" }, { "brand": "Audi", "model": "R8" }, { "brand": "Volvo", "model": "v40" }, { "brand": "BMW", "model": "5 Series" }], combined = []; data.forEach(function (a) { !combined.some(function (b) { if (a.brand === b.brand) { !~b.model.indexOf(a.model) && b.model.push(a.model); return true; } }) && combined.push({ brand: a.brand, model: [a.model] }); }) document.write('<pre>' + JSON.stringify(combined, 0, 4) + '</pre>');

I suggest another optimum way:我建议另一种最佳方式:

 var arr = [ { "brand": "Audi", "model": "A4" }, { "brand": "Audi", "model": "A6" }, { "brand": "BMW", "model": "Z4" }, { "brand": "Audi", "model": "R8" }, { "brand": "Volvo", "model": "v40" }, { "brand": "BMW", "model": "5 Series" } ]; var copy = arr.slice(0); var res = []; while (copy.length) {//it stop when copy is empty var a = {models: []};//create object var item = copy.shift();//item = copy[0], then copy[0] has deleted a.brand = item.brand;//current brand a.models.push(item.model);//first model has added copy.forEach(function (e, i) { if (e.brand === a.brand) {//find other items which they equal current brand a.models.push(e.model);//another models have added to models copy.splice(i, 1);//copy[i] has deleted } }); res.push(a);//current brand item has added } console.log(res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM