Programing

배열에서 속성 값을 합산하는 더 좋은 방법

lottogame 2020. 7. 9. 08:19
반응형

배열에서 속성 값을 합산하는 더 좋은 방법


나는 이와 같은 것을 가지고있다 :

$scope.traveler = [
            {  description: 'Senior', Amount: 50},
            {  description: 'Senior', Amount: 50},
            {  description: 'Adult', Amount: 75},
            {  description: 'Child', Amount: 35},
            {  description: 'Infant', Amount: 25 },
];

이제이 배열의 총량을 얻으려면 다음과 같이하고 있습니다.

$scope.totalAmount = function(){
       var total = 0;
       for (var i = 0; i < $scope.traveler.length; i++) {
              total = total + $scope.traveler[i].Amount;
            }
       return total;
}

하나의 배열 일 때 쉽지만 다른 속성 이름을 가진 다른 배열이 있습니다.

이런 식으로 할 수 있다면 더 행복 할 것입니다.

$scope.traveler.Sum({ Amount });

그러나 나는 이것을 다음과 같이 재사용 할 수있는 방법으로 이것을 통과하는 방법을 모른다.

$scope.someArray.Sum({ someProperty });

대답

@ gruff-bunny 제안을 사용하기로 결정 했으므로 원시 객체 (배열)의 프로토 타이핑을 피하십시오

방금 배열의 유효성을 검사하는 그의 대답을 약간 수정했으며 합계 값이 null이 아닙니다. 이것은 최종 구현입니다.

$scope.sum = function (items, prop) {
    if (items == null) {
        return 0;
    }
    return items.reduce(function (a, b) {
        return b[prop] == null ? a : a + b[prop];
    }, 0);
};

업데이트 된 답변

배열 프로토 타입에 함수를 추가하는 모든 단점으로 인해이 질문에 원래 요청 된 구문과 구문을 유지하는 대안을 제공하기 위해이 답변을 업데이트하고 있습니다.

class TravellerCollection extends Array {
    sum(key) {
        return this.reduce((a, b) => a + (b[key] || 0), 0);
    }
}
const traveler = new TravellerCollection(...[
    {  description: 'Senior', Amount: 50},
    {  description: 'Senior', Amount: 50},
    {  description: 'Adult', Amount: 75},
    {  description: 'Child', Amount: 35},
    {  description: 'Infant', Amount: 25 },
]);

console.log(traveler.sum('Amount')); //~> 235

원래 답변

배열이므로 배열 프로토 타입에 함수를 추가 할 수 있습니다.

traveler = [
    {  description: 'Senior', Amount: 50},
    {  description: 'Senior', Amount: 50},
    {  description: 'Adult', Amount: 75},
    {  description: 'Child', Amount: 35},
    {  description: 'Infant', Amount: 25 },
];

Array.prototype.sum = function (prop) {
    var total = 0
    for ( var i = 0, _len = this.length; i < _len; i++ ) {
        total += this[i][prop]
    }
    return total
}

console.log(traveler.sum("Amount"))

바이올린 : http://jsfiddle.net/9BAmj/


나는이 질문에 대한 대답이 허용된다는 것을 알고 있지만 array.reduce 을 사용하는 대안을 사용 하여 배열을 합산하는 것이 축소의 전형적인 예라는 것을 알았습니다.

$scope.sum = function(items, prop){
    return items.reduce( function(a, b){
        return a + b[prop];
    }, 0);
};

$scope.travelerTotal = $scope.sum($scope.traveler, 'Amount');

Fiddle


또 다른 특징은 nativeJavaScript 기능 Map이며 Reduce(Map and Reduce는 많은 언어의 강국입니다)입니다.

var traveler = [{description: 'Senior', Amount: 50},
                {description: 'Senior', Amount: 50},
                {description: 'Adult', Amount: 75},
                {description: 'Child', Amount: 35},
                {description: 'Infant', Amount: 25}];

function amount(item){
  return item.Amount;
}

function sum(prev, next){
  return prev + next;
}

traveler.map(amount).reduce(sum);
// => 235;

// or use arrow functions
traveler.map(item => item.Amount).reduce((prev, next) => prev + next);

참고 : 더 작은 기능을 분리하여 다시 사용할 수 있습니다.

// Example of reuse.
// Get only Amounts greater than 0;

// Also, while using Javascript, stick with camelCase.
// If you do decide to go against the standards, 
// then maintain your decision with all keys as in...

// { description: 'Senior', Amount: 50 }

// would be

// { Description: 'Senior', Amount: 50 };

var travelers = [{description: 'Senior', amount: 50},
                {description: 'Senior', amount: 50},
                {description: 'Adult', amount: 75},
                {description: 'Child', amount: 35},
                {description: 'Infant', amount: 0 }];

// Directly above Travelers array I changed "Amount" to "amount" to match standards.

function amount(item){
  return item.amount;
}

travelers.filter(amount);
// => [{description: 'Senior', amount: 50},
//     {description: 'Senior', amount: 50},
//     {description: 'Adult', amount: 75},
//     {description: 'Child', amount: 35}];
//     Does not include "Infant" as 0 is falsey.

I always avoid changing prototype method and adding library so this is my solution:

Using reduce Array prototype method is sufficient

// + operator for casting to Number
items.reduce((a, b) => +a + +b.price, 0);

I thought I'd drop my two cents on this: this is one of those operations that should always be purely functional, not relying on any external variables. A few already gave a good answer, using reduce is the way to go here.

Since most of us can already afford to use ES2015 syntax, here's my proposition:

const sumValues = (obj) => Object.keys(obj).reduce((acc, value) => acc + obj[value], 0);

We're making it an immutable function while we're at it. What reduce is doing here is simply this: Start with a value of 0 for the accumulator, and add the value of the current looped item to it.

Yay for functional programming and ES2015! :)


You can do the following:

$scope.traveler.map(o=>o.Amount).reduce((a,c)=>a+c);

Alternative for improved readability and using Map and Reduce:

const traveler = [
    {  description: 'Senior', amount: 50 },
    {  description: 'Senior', amount: 50 },
    {  description: 'Adult', amount: 75 },
    {  description: 'Child', amount: 35 },
    {  description: 'Infant', amount: 25 },
];

const sum = traveler
  .map(item => item.amount)
  .reduce((prev, curr) => prev + curr, 0);

재사용 가능한 기능 :

const calculateSum = (obj, field) => obj
  .map(items => items.attributes[field])
  .reduce((prev, curr) => prev + curr, 0);

이것이 아직 언급되지 않았습니다. 그러나 그것에 대한 lodash 기능이 있습니다. 값이 합산 할 속성 아래의 스 니펫은 'value'입니다.

_.sumBy(objects, 'value');
_.sumBy(objects, function(o) { return o.value; });

둘 다 작동합니다.


Array.prototype.forEach ()를 사용할 수도 있습니다

let totalAmount = 0;
$scope.traveler.forEach( data => totalAmount = totalAmount + data.Amount);
return totalAmount;

다음은 ES6 화살표 기능을 사용하는 하나의 라이너입니다.

const sumPropertyValue = (items, prop) => items.reduce((a, b) => a + b[prop], 0);

// usage:
const cart_items = [ {quantity: 3}, {quantity: 4}, {quantity: 2} ];
const cart_total = sumPropertyValue(cart_items, 'quantity');

Javascript를 사용하여 객체 배열을 합산하는 방법

const traveler = [
  {  description: 'Senior', Amount: 50},
  {  description: 'Senior', Amount: 50},
  {  description: 'Adult', Amount: 75},
  {  description: 'Child', Amount: 35},
  {  description: 'Infant', Amount: 25 }
];

const traveler = [
    {  description: 'Senior', Amount: 50},
    {  description: 'Senior', Amount: 50},
    {  description: 'Adult', Amount: 75},
    {  description: 'Child', Amount: 35},
    {  description: 'Infant', Amount: 25 },
];
function sum(arrayData, key){
   return arrayData.reduce((a,b) => {
  return {Amount : a.Amount + b.Amount}
})
}
console.log(sum(traveler))
`


이미 jquery를 사용하고있었습니다. 그러나 나는 그것의 직관적 인 것만으로 충분하다고 생각합니다.

var total_amount = 0; 
$.each(traveler, function( i, v ) { total_amount += v.Amount ; });

이것은 기본적으로 @akhouri의 대답의 짧은 버전입니다.


더 유연한 솔루션은 다음과 같습니다.

function sumOfArrayWithParameter (array, parameter) {
  let sum = null;
  if (array && array.length > 0 && typeof parameter === 'string') {
    sum = 0;
    for (let e of array) if (e && e.hasOwnProperty(parameter)) sum += e[parameter];
  }
  return sum;
}

합계를 얻으려면 다음과 같이 간단히 사용하십시오.

let sum = sumOfArrayWithParameter(someArray, 'someProperty');

참고 URL : https://stackoverflow.com/questions/23247859/better-way-to-sum-a-property-value-in-an-array

반응형