接收条件推送数组的重复结果

时间:2018-11-29 16:29:43

标签: arrays conditional grouping

我试图根据它们的公共对象键将对象数组转换为新的对象数组,并对每个对象的所有其他属性求和。但是我不知道为什么我不断收到带有正确项目编号但重复项目(带有最后一个新对象)的数组。这是代码:

function(){
    const list = [
        {
            amount: 0,
            date: '2018-11-29',
            source: 'a'
        }
        {
            amount: 2,
            date: '2018-11-29',
            source: 'b'
        }
        {
            amount: 3,
            date: '2018-11-30',
            source: 'b'
        }
    ]
    const daysGroup = []

    let dayGroup = {
            date: list[0].date,
            a: '',
            b: ''
        }

    for (let i = 0; i < list.length; i++) {
        

        if(dayGroup.date === list[i].date){
            switch (list[i].source) {
                case 'a':
                    dayGroup.a += Number(list[i].amount)
                    break;
                case 'b':
                    dayGroup.b += Number(list[i].amount)
                    break;

                default:
                    break;
            }
            console.log(dayGroup) 

        } else{
            console.log(dayGroup)
            daysGroup.unshift(dayGroup)

            dayGroup.date = list[i].date
            switch (list[i].source) {
                case 'a':
                    dayGroup.a += Number(list[i].amount)
                    break;
                case 'b':
                    dayGroup.b += Number(list[i].amount)
                    break;

                default:
                    break;
            } 
        } 
        
    }
    console.log(daysGroup) 
    return daysGroup
}

这是我得到的结果:

[
    {
    date: '2018-11-30',
    a: 0,
    b: 3
    },
    {
    date: '2018-11-30',
    a: 0,
    b: 3
    },
    {
    date: '2018-11-30',
    a: 0,
    b: 3
    }
]

在堆栈中进行搜索时,我意识到有些人已经通过将新的伪对象放入for循环中解决了此问题。但这弄乱了我想要的结果。

我的目标是获得这样的结果:

[
    {
    date: '2018-11-29',
    a: 0,
    b: 2
    },
    {
    date: '2018-11-30',
    a: 0,
    b: 3
    }
]

1 个答案:

答案 0 :(得分:0)

您可以使用Array.reduce以更简洁的方式解决此问题:

function difference(object, base) {
    function changes(object, base) {
        return _.transform(object, function(result, value, key) {
            if (!_.isEqual(value, base[key])) {
                result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
            }
        });
    }
    return changes(object, base);
}


var xml1 = `<?xml version=\"1.0\" encoding=\"UTF-8\"?>
           <envelope>
            <body>
                <d:site_name>Bondi Junction</d:site_name>
                <d:country>AU</d:country>
                <d:regio>NSW</d:regio>
                <d:contact>123456789</d:contact>
            </body>
           </envelope>
`;
var xml2 = `<?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <envelope>
             <body>
                <d:country>AU</d:country>
                <d:region>NSW</d:region>
             </body>
           </envelope>
`;

var jsonObject1 = xml2Json(xml1);
var jsonObject2 = xml2Json(xml2);
var diff = difference(jsonObject1, jsonObject2);

console.log(diff);

pm.test("Test for any differences in the 2 payloads", function() {
    //diff object will be empty in case of no differences
    pm.expect(_.isEmpty(diff)).to.eql(true);

});


pm.test("Test for the value of country in 2 payloads ", function() {
    pm.expect(jsonObject1.envelope.body.country).to.eql(jsonObject2.envelope.body.country);
});

想法是const list = [{ amount: 0, date: '2018-11-29', source: 'a' }, { amount: 2, date: '2018-11-29', source: 'b' }, { amount: 3, date: '2018-11-30', source: 'b' } ] const result = list.reduce((r, {date, amount, source}) => { r[date] = r[date] || {date, a: 0, b: 0} r[date][source] += amount return r }, {}) console.log(Object.values(result)) group乘以date