使用另一个对象的键从对象数组创建新数组

时间:2020-06-18 14:01:27

标签: javascript arrays object ecmascript-6

我有一个对象数组“汽车”

let cars = [
{id: 1, name: 'Mercedes', year: '2015'},
{id: 2, name: 'Mercedes', year: '2000'},
{id: 3, name: 'BMW', year: '2010'},
{id: 4, name: 'BMW', year: '2004'},
{id: 4, name: 'BMW', year: '2004'},
{id: 4, name: 'BMW', year: '2004'},
{id: 4, name: 'BMW', year: '2004'},
{id: 4, name: 'BMW', year: '2004'},
{id: 5, name: 'Volvo', year: '2012'},
{id: 6, name: 'Volvo', year: '2014'},
{id: 7, name: 'Volvo', year: '2010'},
{id: 8, name: 'Toyota', year: '2012'},
{id: 8, name: 'Jeep', year: '2011'},
];

和一个包含汽车数量的对象

let obj = {
BMW: 6,
Jeep: 1,
Mercedes: 2,
Toyota: 1,
Volvo: 3,
}

我需要从汽车中创建一个只有对象的一个​​副本的新数组,并将“ quantity”属性添加到适当的对象。我该如何实现?

预期的输出。

newArray = [
    {id: 1, name: 'Mercedes', year: '2015', quantity: 2},
    {id: 3, name: 'BMW', year: '2010', quantity: 6},
    {id: 5, name: 'Volvo', year: '2012', quantity: 3},
    {id: 8, name: 'Toyota', year: '2012', quantity: 1},
    {id: 8, name: 'Jeep', year: '2011', quantity: 1},
]

5 个答案:

答案 0 :(得分:2)

您可以通过将数组与哈希表一起直接计数。

let cars = [{ id: 1, name: 'Mercedes', year: '2015' }, { id: 2, name: 'Mercedes', year: '2000' }, { id: 3, name: 'BMW', year: '2010' }, { id: 4, name: 'BMW', year: '2004' }, { id: 4, name: 'BMW', year: '2004' }, { id: 4, name: 'BMW', year: '2004' }, { id: 4, name: 'BMW', year: '2004' }, { id: 4, name: 'BMW', year: '2004' }, { id: 5, name: 'Volvo', year: '2012' }, { id: 6, name: 'Volvo', year: '2014' }, { id: 7, name: 'Volvo', year: '2010' }, { id: 8, name: 'Toyota', year: '2012' }, { id: 8, name: 'Jeep', year: '2011' }],
    result = Object.values(cars.reduce((r, o) => {
        r[o.name] = r[o.name] || { ... o, quantity: 0 };
        r[o.name].quantity++;
        return r;
    }, []));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

使用来自cars对象的所有项目和来自obj的计数(没有任何重复)创建一个新数组。

let cars = [{"id":1,"name":"Mercedes","year":"2015"},{"id":2,"name":"Mercedes","year":"2000"},{"id":3,"name":"BMW","year":"2010"},{"id":4,"name":"BMW","year":"2004"},{"id":4,"name":"BMW","year":"2004"},{"id":4,"name":"BMW","year":"2004"},{"id":4,"name":"BMW","year":"2004"},{"id":4,"name":"BMW","year":"2004"},{"id":5,"name":"Volvo","year":"2012"},{"id":6,"name":"Volvo","year":"2014"},{"id":7,"name":"Volvo","year":"2010"},{"id":8,"name":"Toyota","year":"2012"},{"id":8,"name":"Jeep","year":"2011"}];
let obj = {BMW: 6,Jeep: 1,Mercedes: 2,Toyota: 1,Volvo: 3}
let newObj = [];
let duplicateCheck = []
cars.forEach(function(item) {
  if (!duplicateCheck.includes(item.name)) {
    duplicateCheck.push(item.name);
    item.quantity = obj[item.name];
    newObj.push(item);
  }
});
console.log(newObj);

答案 2 :(得分:1)

为了提出新的数组,需要分两个步骤进行处理:

  1. 保持第一个car品牌的独特汽车品牌
  2. 将数量从car添加到obj

let cars=[
  {id:1,name:'Mercedes',year:'2015'},
  {id:2,name:'Mercedes',year:'2000'},
  {id:3,name:'BMW',year:'2010'},
  {id:4,name:'BMW',year:'2004'},
  {id:4,name:'BMW',year:'2004'},
  {id:4,name:'BMW',year:'2004'},
  {id:4,name:'BMW',year:'2004'},
  {id:4,name:'BMW',year:'2004'},
  {id:5,name:'Volvo',year:'2012'},
  {id:6,name:'Volvo',year:'2014'},
  {id:7,name:'Volvo',year:'2010'},
  {id:8,name:'Toyota',year:'2012'},
  {id:8,name:'Jeep',year:'2011'}
];
let obj = {
  BMW:6,
  Jeep:1,
  Mercedes:2,
  Toyota:1,
  Volvo:3,
};

/* The first step:
you can use .filter to 
only return the car you are looking for.
*/
let uniqueCarBrand = [];
let newArray = cars.filter((car) => {
  /* Check if car name already in the newArray */
  if (uniqueCarBrand.indexOf(car.name) >= 0) return false;
  uniqueCarBrand.push(car.name);
  return true;
});

/* Second step:
you will need to attach your quantity to
the array of cars you just created.
*/
let result = newArray.map((car) => {
  /* Check if obj has brand name in it, if not, return 0; */
  car.quantity = obj[car.name] || 0;
  return car;
});

console.log(result);

答案 3 :(得分:0)

您可以使用两个循环来匹配两个数组中的对象:

let output = [];
// For each key in your quantity object
for (let model in obj) {
    // For each car object in your cars array
    for (let car of cars) {
        // This will match the first car in your cars array
        if (car.name === model) {
            // Add quantity to your car object and add it to your result set
            car.quantity = obj[model];
            output.push(car);
            // This break will continue to the next model car to ensure you only have one result per model
            break;
        }
    }
}

答案 4 :(得分:0)

使用ES6模块

let cars = [
  {id: 1, name: 'Mercedes', year: '2015'},
  {id: 2, name: 'Mercedes', year: '2000'},
  {id: 3, name: 'BMW', year: '2010'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 4, name: 'BMW', year: '2004'},
  {id: 5, name: 'Volvo', year: '2012'},
  {id: 6, name: 'Volvo', year: '2014'},
  {id: 7, name: 'Volvo', year: '2010'},
  {id: 8, name: 'Toyota', year: '2012'},
  {id: 8, name: 'Jeep', year: '2011'},
];
let obj = {
  BMW: 6,
  Jeep: 1,
  Mercedes: 2,
  Toyota: 1,
  Volvo: 3,
}  

let filteredCar = cars.filter((curr, index) => {
  let _cars = JSON.stringify(curr);
  return index === cars.findIndex(obj => {
    return JSON.stringify(obj) === _cars;
  })
}).map(currCar => {
  currCar.quantity = obj[currCar.name];
                   return currCar});

console.log(filteredCar);