我正在使用 JSON 路徑來做類似的事情:
我復制了 JSON 路徑示例,但修改了價格欄位以表示價格同比(數字到陣列)。
{
"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
}
我想找到所有書籍的同比總價。
我可以使用以下方法獲得一個陣列陣列(可以說是 res): $.store.book[*].price
輸出:
[
[ 1, 2, 3 ],
[ 1, 2, 3 ],
[ 1, 2, 3 ],
[ 1, 2, 3 ]
]
我想進一步將此輸出(按總和)減少到:
[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)
有沒有辦法使用 jsonpath(首選)/任何其他 JavaScript 語法來實作這一點?
uj5u.com熱心網友回復:
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 ]
第一個映射轉置行和列,第二個映射總結轉置后的行。
uj5u.com熱心網友回復:
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)
uj5u.com熱心網友回復:
您可以通過將第一行列映射到每個后續行的減少來對矩陣中的每一列求和。
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; }
uj5u.com熱心網友回復:
您可以將 Array.prototype.reduce() 與逗號運算子配對使用。
const o = { "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(o.map(x=>x.price).reduce((x,y)=>(x[0] =y[0],x[1] =y[1],x[2] =y[2],x)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/378025.html
標籤:javascript 数组 路径
下一篇:值相同但不相等
