How to use a parameter from the list of objects based on another parameter? (Javascript)

I'm not sure if I can explain this correctly, but here it is. Say I have this:

List = {
    0: {
        payment: 1,
        value: 10
    },
    1: {
        payment: 2,
        value: 15
    },
    2: {
        payment: 1,
        value: 12
    },
    3: {
        payment: 2,
        value: 13
    }
}

How to sum the values ​​for two payments separately? aka sumforpayment1= 10 +12andsumforpayment2= 15 + 13

+4
source share
4 answers

You can use Object.keys()and reduce()return an object with an amount for each payment.

var data = {
  0: {
    payment: 1,
    value: 10
  },
  1: {
    payment: 2,
    value: 15
  },
  2: {
    payment: 1,
    value: 12
  },
  3: {
    payment: 2,
    value: 13
  }
}

var result = Object.keys(data).reduce(function(r, e) {
  var key = 'sumforpayment' + data[e].payment;
  r[key] = (r[key] || 0) + data[e].value;
  return r;
}, {})

console.log(result)
Run codeHide result
+4
source

I think you need an array of objects. The object in this case is not needed.

List = [
{
        payment: 1,
        value: 10
},
{
        payment: 2,
        value: 15
},
{
        payment: 1,
        value: 12
},
{
        payment: 2,
        value: 13
}
];

var List2 = {};

List.map(function(o) {
 if(!List2['sumforpayment' + o.payment]) {
  List2['sumforpayment' + o.payment] = o.value;
 }
 else {
 List2['sumforpayment' + o.payment] += o.value;
 }
});

console.log(List2);
</script>
Run codeHide result
+1
source

, .

var list = {
    0: {
        payment: 1,
        value: 10
    },
    1: {
        payment: 2,
        value: 15
    },
    2: {
        payment: 1,
        value: 12
    },
    3: {
        payment: 2,
        value: 13
    }
},
     res = [];
list[Symbol.iterator] = function*(){
                          var oks = Object.keys(this),
                          payment = {1:0,2:0};
                          for (var key of oks) this[key].payment === 1 ? payment["1"] += this[key].value
                                                                       : payment["2"] += this[key].value;
                          yield payment;
                        };
res = [...list];
console.log(res);
Hide result
+1

-, , JavaScript-, .

.

You can create a utility function that iterates over your object, looking for the value of the payment identifier and calculates the total amount.

How it works:

  • Iterate over object properties with Object.keys().
  • If the property value paymentpasses the "selector" ( paymentNumber)
  • Summarize its values ​​to return the final result.

var list = {
  0: {
    payment: 1,
    value: 10
  },
  1: {
    payment: 2,
    value: 15
  },
  2: {
    payment: 1,
    value: 12
  },
  3: {
    payment: 2,
    value: 13
  }
}

var calculatePaymentSum = function(paymentNumber, data) {
  var result = 0;
  Object.keys(data).forEach(function(key) {
    if (data[key].payment === paymentNumber) {
      result += data[key].value;
    }
  }, this);
  return result;
};

console.log('sum of payment 1 = ', calculatePaymentSum(1, list));
console.log('sum of payment 2 = ', calculatePaymentSum(2, list));
Run codeHide result
0
source

Source: https://habr.com/ru/post/1650909/


All Articles