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

时间:2015-10-12 08:49:07

标签: javascript json angularjs

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

但问题是,如果我有3款车型用于奥迪,则该列表会一次又一次地重复该品牌,从而填充我的列表中的重复值。所以我需要制作一个像这样的json对象: 我只想使用javascript或angularjs创建一个json对象:

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

5 个答案:

答案 0 :(得分:1)

鉴于您的意见:

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

您可以使用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;

答案 1 :(得分:1)

你可以使用角度js或javascript轻松制作JSON。您可以在角度js中使用以下方法。在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);
});

现在,returnObject将保存您想要的JSON对象。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

仅包含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>');

答案 4 :(得分:0)

我建议另一种最佳方式:

&#13;
&#13;
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);
&#13;
&#13;
&#13;