【问题标题】:I want to merge values in an array if they have same id [duplicate]如果它们具有相同的ID,我想合并数组中的值[重复]
【发布时间】:2019-12-06 17:58:51
【问题描述】:

我有一个具有相同 ID 和不同值的数组对象。我需要一个具有相同 id 和不同值合并到 id 的输出。

输入:

let data = [{
    id: 1,
    value: 'Honda'
  },
  {
    id: 2,
    value: 'Fiat'
  },
  {
    id: 2,
    value: 'Porche'
  },
  {
    id: 1,
    value: 'Benz'
  }
];

输出:

result = [{
      id: 1,
      value: ['Honda', 'Benz']
    }, {
      id: 2,
      value: ['Fiat', 'Porche']
    }

【问题讨论】:

  • 太好了……你有一个目标。那么显示您如何尝试解决此问题的代码以及您遇到的问题的概述在哪里?

标签: javascript


【解决方案1】:

希望对您有所帮助。但是这个问题是重复的

let data = [{
    id: 1,
    value: 'Honda'
  },
  {
    id: 2,
    value: 'Fiat'
  },
  {
    id: 2,
    value: 'Porche'
  },
  {
    id: 1,
    value: 'Benz'
  }
];

var output = [];

data.forEach(function(item) {
  var existing = output.filter(function(v, i) {
    return v.id == item.id;
  });
  if (existing.length) {
    var existingIndex = output.indexOf(existing[0]);
    output[existingIndex].value = output[existingIndex].value.concat(item.value);
  } else {
    if (typeof item.value == 'string')
      item.value = [item.value];
    output.push(item);
  }
});

console.dir(output);

【讨论】:

    【解决方案2】:

    const data = [{
        id: 1,
        value: 'Honda'
      },
      {
        id: 2,
        value: 'Fiat'
      },
      {
        id: 2,
        value: 'Porche'
      },
      {
        id: 1,
        value: 'Benz'
      }
    ];
    
    const expectedResult = [{
          id: 1,
          value: ['Honda', 'Benz']
        }, {
          id: 2,
          value: ['Fiat', 'Porche']
        }
     ];
     
     const result = [];
     data.forEach((e, i) => {
        const indexOfExisting = result.findIndex(x => x.id === e.id);
        if (indexOfExisting === -1) {
          result.push({
              id: e.id,
              value: [e.value]
          })
        } else {
          result[indexOfExisting].value.push(e.value);
        }
     });
     
     // console.log(result)
     console.log(expectedResult)

    【讨论】:

      【解决方案3】:

      你可以使用Array.prototype.reduce()来实现。

      let data = [{
          id: 1,
          value: 'Honda'
        },
        {
          id: 2,
          value: 'Fiat'
        },
        {
          id: 2,
          value: 'Porche'
        },
        {
          id: 1,
          value: 'Benz'
        }
      ];
      
      
      let result = data.reduce((acc, ele) => {
      
        let filtered = acc.filter(el => el.id == ele.id)
        if (filtered.length > 0) {
      
          filtered[0]["value"].push(ele.value);
      
        } else {
          element = {};
          element["id"] = ele.id;
          element["value"] = []
          element["value"].push(ele.value);
          acc.push(element)
      
        }
      
        return acc;
      }, [])
      console.log(result)

      【讨论】:

      • 如果它对你有用,请接受答案。谢谢@ShrutiMalkood
      猜你喜欢
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 2014-10-08
      相关资源
      最近更新 更多