javascript - 将半重复的对象合并到对象数组中

标签 javascript jquery arrays sorting object

如果欧姆值、容差和瓦数值与之前检查的对象相同,我想合并对象的数量值。 起始对象:

var x = [{
    date: "2020",
    ohmage: "1k45",
    quantity: 5000,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "9k34",
    quantity: 1000,
    tolerance: 2,
    wattage: 0.125
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3000,
    tolerance: 2,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3500,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 500,
    tolerance: 5,
    wattage: 0.5
}];

想要的对象:

var x = [{
    date: "2020",
    ohmage: "1k45",
    quantity: 8500,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "9k34",
    quantity: 1000,
    tolerance: 2,
    wattage: 0.125
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3000,
    tolerance: 2,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 500,
    tolerance: 5,
    wattage: 0.5
}];

我看了一下Merge duplicate objects in array of objects并重新编写了该函数,但它没有组合所有应该组合的对象。 我当前的功能:

var seen = {};
array = array.filter(entry => {
    var previous;
    // Have we seen this ohmage before?
    if (seen.hasOwnProperty(entry.ohmage)) {
        if (entry.tolerance == seen[entry.ohmage].tolerance && entry.wattage == seen[entry.ohmage].wattage) {
            console.log(true)
            // Yes, grab it and add this quantity to it
            previous = seen[entry.ohmage];
            previous.quantity.push(entry.quantity);

            // Don't keep this entry, we've merged it into the previous one
            return false;
        }
    }
    // entry.quantity probably isn't an array; make it one for consistency
    if (!Array.isArray(entry.quantity)) {
        entry.quantity = [entry.quantity];
    }
    // Remember that we've seen it
    seen[entry.ohmage] = entry;

    // Keep this one, we'll merge any others that match into it
    return true;
});

最佳答案

array.filter 函数无法修改数组元素,它只是保留回调函数返回 true 的元素。要合并元素,您可以使用reduce函数:

const result = array.reduce((acc, entry) =>{
if (acc.filter(item => item.ohmage===entry.ohmage && item.tolerance===entry.tolerance && item.wattage===entry.wattage).length > 0)
{  
  // If an entry with parameters already exists
  let idx = acc.findIndex(item => item.ohmage===entry.ohmage && item.tolerance===entry.tolerance && item.wattage===entry.wattage)
  //Merge items by sum quantities as in example
  acc[idx] = {...acc[idx], quantity: acc[idx].quantity + entry.quantity}
  return acc
}

  // Otherwise adds entry to accumulator as it is
 return [...acc, entry]

}, [])

console.log(result)
<script>
var array = [{
    date: "2020",
    ohmage: "1k45",
    quantity: 5000,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "9k34",
    quantity: 1000,
    tolerance: 2,
    wattage: 0.125
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3000,
    tolerance: 2,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3500,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 500,
    tolerance: 5,
    wattage: 0.5
}];

</script>

关于javascript - 将半重复的对象合并到对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61198272/

相关文章:

javascript - onchange 函数由于某种原因未运行

javascript - 需要时水平滚动列表居中

c# - 对返回类型使用 ReadOnlyCollection<T> 与 List<T> 与数组

javascript - 为什么这些数组扩展在运行时不可用

javascript - 使用套接字时返回 promise

javascript - Jquery按钮文字效果

html - 不同国家的 jquery cookies 链接

javascript - 如何重构这个重复的 jQuery 代码

java - 文本文件解析上的数组 IndexOutOfBoundsException

C++ 将 UTF-8 字符串迭代或拆分为符号数组?