sum of specific key for multiple similar keys in array of object - JavaScript

Mangrio

I have an array of objects like the following:

[
    {
        'Product': 'P1',
        'Price': 150,
        'Location': 1,
    },
    {
        'Product': 'P1',
        'Price': 100,
        'Location': 1,
    },
    {
        'Product': 'P1',
        'Price': 200,
        'Location': 2,
    },
    {
        'Product': 'P2',
        'Price': 10,
        'Location': 1,
    },
    {
        'Product': 'P2',
        'Price': 130,
        'Location': 1,
    },
    {
        'Product': 'P3',
        'Price': 40,
        'Location': 1,
    }
]

I need to add up all the prices for objects with the same product and same location. For the example above the result would be:

[
   {
        'Product': 'P1',
        'Price': 250, // price is the sum of both similar in location and product
        'Location': 1,
    },
    {
        'Product': 'P1',
        'Price': 200,
        'Location': 2, // same product but different location
    },
    {
        'Product': 'P2',
        'Price': 140, //sum of same
        'Location': 1, 
    },
    {
        'Product': 'P3',
        'Price': 40,
        'Location': 1,
    },
]

I have searched several similar issues, but those were dealing with only one key to check for, I have different keys (product and location - may be more than 2 in the future) to identify as different.

William Wang

const input = [
    {
        'Product': 'P1',
        'Price': 150,
        'Location': 1,
    },
    {
        'Product': 'P1',
        'Price': 100,
        'Location': 1,
    },
    {
        'Product': 'P1',
        'Price': 200,
        'Location': 2,
    },
    {
        'Product': 'P2',
        'Price': 10,
        'Location': 1,
    },
    {
        'Product': 'P2',
        'Price': 130,
        'Location': 1,
    },
    {
        'Product': 'P3',
        'Price': 40,
        'Location': 1,
    }
]

const output = []
input.forEach(item => {
  const index = output.findIndex(o => o.Product === item.Product && o.Location === item.Location);
  if (index === -1) {
    output.push(item);
  } else {
    output[index].Price += item.Price;
  }
});
console.log(output);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Sorting array of object by specific key

분류에서Dev

Changing multiple array keys/names in JavaScript

분류에서Dev

Comparing Two Arrays that don't have standard keys in the object key/value pairs, pushing missing values to first array in javascript

분류에서Dev

Array slice for specific keys

분류에서Dev

List Object with multiple "keys" in it

분류에서Dev

Angular, watch specific keys in object?

분류에서Dev

how to sum up all values with same keys in an object inside an array and get their average

분류에서Dev

javascript sum multidimensional array

분류에서Dev

Filter an array for a subset of object keys

분류에서Dev

Update specific object in array

분류에서Dev

JSON / JavaScript get object keys

분류에서Dev

INSERT INTO .. ON DUPLICATE KEY UPDATE with multiple keys

분류에서Dev

PHP How to sum values of the array of the same key

분류에서Dev

Create Javascript object from Keys arrays

분류에서Dev

Pass an object into an array in JavaScript

분류에서Dev

Node js object array common value sum

분류에서Dev

OrderBy in Angular.js for (key,value) object in reverse based on keys

분류에서Dev

Can I add an object key to an array?

분류에서Dev

fetch json object from json array with a key

분류에서Dev

Pointfree Join Array to String, by key in object, in Ramda

분류에서Dev

Insert at a specific location in Ember.Array Object

분류에서Dev

Object insert issue in JavaScript array

분류에서Dev

Sort javascript object with array as value

분류에서Dev

Javascript - Defining a Object with Array Properties

분류에서Dev

Removing specific key-value pairs from a document/object

분류에서Dev

Word frequency for array of key/values on javascript

분류에서Dev

Convert array to Hashmap (key value pair) javascript

분류에서Dev

Completion: Multiple similar options

분류에서Dev

$ lookup in multiple array object both collection

Related 관련 기사

  1. 1

    Sorting array of object by specific key

  2. 2

    Changing multiple array keys/names in JavaScript

  3. 3

    Comparing Two Arrays that don't have standard keys in the object key/value pairs, pushing missing values to first array in javascript

  4. 4

    Array slice for specific keys

  5. 5

    List Object with multiple "keys" in it

  6. 6

    Angular, watch specific keys in object?

  7. 7

    how to sum up all values with same keys in an object inside an array and get their average

  8. 8

    javascript sum multidimensional array

  9. 9

    Filter an array for a subset of object keys

  10. 10

    Update specific object in array

  11. 11

    JSON / JavaScript get object keys

  12. 12

    INSERT INTO .. ON DUPLICATE KEY UPDATE with multiple keys

  13. 13

    PHP How to sum values of the array of the same key

  14. 14

    Create Javascript object from Keys arrays

  15. 15

    Pass an object into an array in JavaScript

  16. 16

    Node js object array common value sum

  17. 17

    OrderBy in Angular.js for (key,value) object in reverse based on keys

  18. 18

    Can I add an object key to an array?

  19. 19

    fetch json object from json array with a key

  20. 20

    Pointfree Join Array to String, by key in object, in Ramda

  21. 21

    Insert at a specific location in Ember.Array Object

  22. 22

    Object insert issue in JavaScript array

  23. 23

    Sort javascript object with array as value

  24. 24

    Javascript - Defining a Object with Array Properties

  25. 25

    Removing specific key-value pairs from a document/object

  26. 26

    Word frequency for array of key/values on javascript

  27. 27

    Convert array to Hashmap (key value pair) javascript

  28. 28

    Completion: Multiple similar options

  29. 29

    $ lookup in multiple array object both collection

뜨겁다태그

보관