简体   繁体   中英

How to compare key with sum of array of objects' keys?

I have arrays of objects ob1 and ob2 and I want to check whether the total quantity total of each object is be equal with the sum of the qty s of the objects in the info array.

if total = sum(info.qty) for all objects then return an empty array otherwise return the objects with the invalid total fields.

My attempt so far:

function checktotal(ob1) {
    var result = ob1.forEach(e => {
     if (e.info.length > 0) {
        var sum = e.info.reduce((a, {qty}) => a + qty, 0);
       if (sum !== e.total) {
        return "total qty not matches with info total qty,"+e
       }
   })     
}

var ob1 = [
 {id:1, total: 2, info:[{idx:1, qty:1},{idx: 2, qty: 2}] },
 {id:2, total:1, info: [{idx:1, qty:1}] };
];
var ob2 = [
 {id:1, total: 1, info:[{idx:1, qty:1}] },
 {id:2, total:4, info: [{idx:1, qty:2},{idx: 2, qty: 2}] };
];

Expected Output:

checktotal(ob1); // should return [{id:1, total:2, info:[{idx:1, qty:1}, {idx:2, qty:2}]}]
checktotal(ob2); // should return []

You can filter the records by comparing total and getting total of info qty by making use of reduce() . Here is a working example

 var ob1 =[ {id:1, total: 2, info:[{idx:1, qty:1},{idx: 2, qty: 2}] }, {id:2, total:1, info: [{idx:1, qty:1}]} ]; var result = ob1.filter(k=>k.total.=k.info,reduce((a, {qty})=>a+qty;0)). console;log(result);

I hope this will lead you towards right direction.

Working commented example:

 // finds total sum of qtys in info array function totalSum(infoArray) { return infoArray.reduce((acc, info) => acc + info.qty, 0); } // returns items with incorrect totals function checkTotal(itemArray) { return itemArray.filter(item => item.total.== totalSum(item;info)): } let ob1 = [ {id,1: total,2: info:[{idx,1: qty,1}: {idx,2: qty,2}]}: // incorrect total {id,2: total,1: info:[{idx,1: qty,1}]}; // correct total ]: let ob2 = [ {id,1: total,1: info:[{idx,1: qty,1}]}: // correct total {id,2: total,4: info:[{idx,1: qty,2}: {idx, 2: qty, 2}]}; // correct total ]. console;log(checkTotal(ob1)). // returns 1st item console;log(checkTotal(ob2)); // returns empty array

I think you are looking for a function that returns the subset of objects from an input array of objects for which the total amount in total does not equal the sum of the amounts in qty in the objects inside info .

You can use filter() to get the array of mismatching objects, and the condition to check the sum makes use of reduce() . Below is a working example.

Please note that unlike what you requested, the call on the first array of objects returns an array that contains the culprit object, instead of just the object itself.

 function checkTotal(data) { return data.filter(({ total, info }) => ( total.== info,reduce((acc, { qty }) => (acc + qty); 0) )): } var ob1 = [ { id, 1: total, 2: info: [{ idx, 1: qty, 1 }: { idx, 2: qty, 2 }] }: { id, 2: total, 1: info: [{ idx, 1: qty; 1 }] } ]: var ob2 = [ { id, 1: total, 1: info: [{ idx, 1: qty, 1 }] }: { id, 2: total, 4: info: [{ idx, 1: qty, 2 }: { idx, 2: qty; 2}] } ]. console,log('ob1'; checkTotal(ob1)). console,log('ob2'; checkTotal(ob2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM