[英]Javascript: How to filter an object array and sum result
我有一個對象數組:
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]
我正在嘗試添加與c
不對應的所有值。
我已經成功地用console.log(test.filter(x => xc> 3))過濾掉了我想要的行。
我也嘗試了數據查詢並將它鏈接.ne(“ c”),但這沒有用。
我設法找到一個對象數組的總和,但是它沒有忽略與“ c”相對應的元素。 該代碼是:
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]
var sumobjects
sumobjects = example.map(y => Object.keys(y).reduce((x,z) => x + y[z], 0));
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sumexample = sumobjects.reduce(reducer)
console.log(sumexample);
我當前的代碼如下所示:
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]
function filtration (arr) {
var filteredsum = 0
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
for(let k = 0; k < arr[i][j].length && arr[i][j] !== "c"; k++)
filteredsum += arr[i][j]
}
}
return(filteredsum);
}
console.log(filtration(example));
答案應該是一個單數,即27
。
我當前擁有的代碼輸出0
,因此我認為與其求和而不是忽略屬性“ c”,而是找到c
,然后從對象數組中刪除整個對象。
編輯:上面的對象數組是我實際使用的對象數組的簡化版本。 實際的對象數組不限於屬性a,b和c。 它具有大約350種不同的屬性。 因此,通過實際聲明a,b和c來添加a,b和c的代碼對我來說不是一個好選擇。
如果只想排除“ c”屬性,但要包括數組對象所有屬性的值之和,則可以這樣做。
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}];
let sum = 0;
example.forEach(obj => {
for (let property in obj) {
if(property !== "c")
sum += obj[property];
}
})
您可以使用下面的函數,並將鍵作為第二個參數的字符串數組省略。 第一個參數將是列表本身。
const sumWithout = (list, without) => {
return list.reduce((acc, item) => {
for (let [key, value] of Object.entries(item)) {
if(!without.includes(key)){
acc = Number(value) + acc;
}
}
return acc;
}, 0)
}
console.log(sumWithout(yourList, ['c', 'foo', '...' ]));
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]
var res = example.reduce((acc, curr) => {
return acc = acc + curr.a + curr.b
}, 0)
您可以通過這種方式減少初始數組並獲得所需的結果。 它以0
作為初始值,然后為數組的每個元素添加a
和b
屬性
如果要除“ c”之外的所有“ a”和“ b”屬性的總和,可以這樣進行:
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}];
let totalSum = 0;
example.forEach(obj => {
let objSum = obj.a + obj.b;
totalSum += objSum;
})
嘗試
let example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]; let keys = Object.keys(example[0]).filter(k=> k!='c'); let sumObj = obj => keys.reduce((a,c)=> a + obj[c],0); let totalSum = example.reduce((a,c)=> a + sumObj(c), 0); console.log(totalSum);
如果要添加對象的除某些屬性之外的所有屬性,可以通過遍歷屬性並檢查它們是否是您要避免的屬性並采取相應措施來將其過濾掉。
無論屬性的數目及其名稱如何,此代碼均有效。 您可以根據需要擴展它以過濾出更多屬性。
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}] const result = example.reduce((acc, cur) => { for (let el in cur) { acc += el !== "c" ? cur[el] : 0 } return acc; }, 0) console.log(result)
希望這可以幫助。
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}];
var sum = 0;
for(var i = 0; i < example.length; i++){
for(var j = 0; j < Object.keys(example[i]).length; j++){
if(Object.keys(example[i])[j] != "c"){
sum += parseInt(example[i][Object.keys(example[i])[j]]);
}
}
}
console.log(sum);
好的,我發現您的現實生活對象很大。 您可以這樣做:
const result = example.map(item => {
// first way (extract a, b)
const {a, b} = item;
// second way (omit c)
const {c, ...rest} = item;
})
那么這里發生了什么。 如果它更容易從最初的對象你提取的屬性,然后你走first way
和回報{a, b}
如果省略屬性更好,那么您就走second way
並返回rest
所以我采取第二種方式繼續執行代碼
const res = example.map(item => {
const {c, ...rest} = item;
return rest
}).reduce((acc, curr) => {
Object.values(curr).forEach(value => acc = acc + value);
return acc
}, 0);
如果您選擇第一種方式-減速器將相同,則僅map
返回會有所不同。
您已經有了添加所有屬性的代碼。
Object.keys(y)
可以獲取對象的所有屬性,reduce可以將它們與y[z]
一起使用,假設z ='a'就像在做ya
:解決方案是在reduce
之前過濾掉不需要的屬性
更改在此行上:
var sumobjects = example.map(y => Object.keys(y) .filter(k => k!=='c'). reduce((x,z)=> x + y [z],0)) ;
這里是完整的代碼:
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]
var sumobjects = example.map(y => Object.keys(y).filter(k=>k!=='c').reduce((x,z) => x + y[z], 0));
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sumexample = sumobjects.reduce(reducer)
console.log(sumexample);
如果要排除多個屬性,則可以創建一個帶有屬性的數組來避免:
var exceptedProperties = ["c","d"];
改變是
var sumobjects = example.map(y => Object.keys(y) .filter(k =>!exceptedProperties.some(p => p === k)). reduce((x,z)=> x + y [z],0));
完整的代碼:
var example = [{a:1, b:2, c:3, d:4}, {a:4, b:5, c:6, d:7}, {a:7, b:8, c:9, d:10}]
var sumobjects = example.map(y => Object.keys(y).filter(k=>!exceptedProperties.some(p=>p===k)).reduce((x,z) => x + y[z], 0));
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sumexample = sumobjects.reduce(reducer)
console.log(sumexample);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.