【问题标题】:Combine semi duplicate objects in array of objects在对象数组中组合半重复对象
【发布时间】:2020-07-26 14:59:55
【问题描述】:

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

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

【问题讨论】:

    标签: javascript jquery arrays sorting object


    【解决方案1】:

    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2019-07-15
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      相关资源
      最近更新 更多