I have an object array:
var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]
I am trying to add all values that don't correspond with c
.
I've managed to filter out a row, which wasn't what I was after, with console.log(test.filter(x => x.c > 3));
I've also tried a data query and chaining .ne("c") to it, but this didn't work.
I have managed to find the sum of an object array, but it doesn't omit the elements corresponding with "c". The code for that is:
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);
My current code is looking like this:
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));
The answer should be one single number, being 27
.
The code I currently have is outputting 0
, so I figure that instead of omitting the property "c" from the sum, it's finding c
and then omitting the entire object from the object array.
EDIT: The object array above is a simplified version of the object array I'm actually working with. The actual object array is not limited to the properties a, b and c. It has about 350 different properties. So code that adds a and b and c by actually stating a, b and c isn't going to be a good option for me.