database javascript: filter and sum array/objects

Hi all,

Say I have a database:

a b c
1 2 3
4 5 6
7 8 9

This is actually an array:

var database = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]

I need to do two things:

  1. I need to filter out particular columns of my database so that it becomes:

a b
1 2
4 5
7 8

which would become the array:

[{a:1, b:2}, {a:4, b:5}, {a:7, b:8}]

  1. Then I want to sum all the remaining values together i.e.: 1 + 2 + 4 + 5 + 7 +8

Any help would be greatly appreciated. Thanks

Query your collection:
https://www.wix.com/corvid/reference/wix-data.html#query
Then use .reduce() method to sum up the desired fields:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Sum_of_values_in_an_object_array

EDIT: More details about what I have in my code so far:

/ so say this is my database (I’m not pulling it from the wix dataset yet - just working with this array as an example to make things simpler):/
var test = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]

// I have managed to filter out a row - I can filter out the first row with:
console.log(test.filter(x => x.c > 3));
// but like I said, I need to filter out a column, not a row.

/ I have also managed to ‘sum’ the objects in the array. But this doesn’t work - it doesn’t sum the numbers - it sums the objects and the below code will produce something like “[object Object][object Object][object Object]” /
const total = (accumulator, currentValue) => accumulator + currentValue;
console.log(test.reduce(total));

Ok so I’m trying to get the query working. I’ve got:

wixData.query(“test”)
.ne(“c”)
.find()
.then( (results) => {
console.log(results.items);
} );

Unfortunately this does not produce [{a:1, b:2}, {a:4, b:5}, {a:7, b:8}]. Instead it comes back with nothing i.e. it has removed the whole object because each has “c” in it.

Also I’m needing to filter out the columns I don’t need as I have 5 columns I don’t need and about 350 columns that I do need.

Also, regarding the reduce link, which mentions summing an object array, it has:

var initialValue = 0;
var sum = [{x: 1}, {x: 2}, {x: 3}].reduce(function (accumulator, currentValue) {
    return accumulator + currentValue.x;
},initialValue)

console.log(sum)

This is the equivalent of summing a column in a database because it’s summing elements corresponding with ‘x’. I need to sum everything (not just one column). I don’t want to be stating every column I want included in the sum because like I said, I will have about 350 columns. I want to create a new array that omits about 5 columns and then sums the remaining elements.

I’ve also tried:

var example = [{a:1, b:2, c:3}, {a:4, b:5, c:6}, {a:7, b:8, c:9}]
var i = 0
const map1 = example.map(x => i += x);
console.log(map1)

But again this produces [object Object][object Object][object Object]

I think I have how to sum the object array elements:

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);

Now just have to figure out how to filter out a column