Home > Mobile >  Json Path sum functions for List of List
Json Path sum functions for List of List

Time:12-10

I am using JSON path to do something similar to this:

I have copied the JSON path example, but modified the price field to represent price Year-over-Year (number to array).

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": [ 1, 2, 3 ]
      },
      {
        "category" :"fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": [ 1, 2, 3 ]
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": [ 1, 2, 3 ]
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": [ 1, 2, 3 ]
      }
    ],
    "bicycle": {
      "color": "red",
      "price": [ 1, 2, 3 ]
    }
  },
  "expensive": 10
}

What I want to find is the year over year total price for all the books.

I can get a Array of Array (lets say res) using: $.store.book[*].price

Output:

[
   [ 1, 2, 3 ],
   [ 1, 2, 3 ],
   [ 1, 2, 3 ],
   [ 1, 2, 3 ]
]

I want to further reduce this output (by sum) to:

[4, 8, 12] // Sum up nth element of each array.
           // (res[0][0]   res[1][0]   res[2][0]   res[3][0] = 4 ... and so on)

Is there a way to achieve this using jsonpath (preferred)/any other JavaScript syntax ?

CodePudding user response:

let data = [
  [ 1, 7, 3 ],
  [ 2, 6, 3 ],
  [ 3, 5, 3 ],
  [ 4, 4, 3 ]
]

let result = data[0].map((_, colIndex) => data.map(row => row[colIndex]))
                    .map(value => value.reduce((acc, value) => acc   value, 0))

console.log(result)   // [ 10, 22, 12 ]

The first map transposes rows and columns, the second map sums up what after the transpose are the rows.

CodePudding user response:

const data = {
  "store": {
    "book": [{
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": [1, 2, 3]
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": [1, 2, 3]
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": [1, 2, 3]
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": [1, 2, 3]
      }
    ]
  }
}

const prices = []

data.store.book.forEach(book => {
  book.price.forEach((price, index) => {
    if (!prices[index]) prices[index] = 0;
    prices[index]  = price;
  })
})

console.log(prices)

CodePudding user response:

You can sum each of the columns in the matrix by mapping the first row columns to a reduction of each of their subsequent rows.

const data = {"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":[1,2,3]},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":[1,2,3]},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":[1,2,3]},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":[1,2,3]}],"bicycle":{"color":"red","price":[1,2,3]}},"expensive":10};

const sumColumns = (matrix) =>
  matrix[0].map((_, col) =>
    matrix.reduce((acc, data, row) => acc   data[col], 0));

const bookPrices = data.store.book.map(({ price }) => price);

const priceSums = sumColumns(bookPrices);

console.log(priceSums); // [ 4, 8, 12 ]
.as-console-wrapper { top: 0; max-height: 100% !important; }

CodePudding user response:

You can use some .map() and Array.prototype.reduce() paired with comma operator.

const a = { "store": { "book": [{ "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": [1, 2, 3] }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": [1, 2, 3] }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": [1, 2, 3] }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": [1, 2, 3] } ], "bicycle": { "color": "red", "price": [1, 2, 3] } }, "expensive": 10 }.store.book

console.log(a.map(x=>x.price).reduce((x,y)=>(y.map((i,z)=>x[z] =y[z]),x)))

  • Related