Estoy usando la ruta JSON para hacer algo similar a esto:
Copié el ejemplo de la ruta JSON, pero modifiqué el campo de precio para representar el precio año tras año (número a matriz).
{ "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": "JRR 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 }Lo que quiero encontrar es el precio total año tras año para todos los libros.
Puedo obtener un Array of Array (digamos res) usando: $.store.book[*].price
Producción:
[ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]Quiero reducir aún más esta salida (por suma) a:
[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)¿Hay alguna manera de lograr esto usando jsonpath (preferido)/cualquier otra sintaxis de JavaScript?
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 ]El primer mapa transpone filas y columnas, el segundo mapa resume lo que después de la transposición son las filas.
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": "JRR 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)Puede usar algunos .map() y Array.prototype.reduce() combinados con el operador de coma.
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": "JRR 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)))