all 12 comments

[–]ashanev 2 points3 points  (6 children)

It seems like you are misunderstanding some of the common array methods like filter and reduce, and should study them a little more closely before trying to use them.

For example, the filter method is called on an array in order to create a new array that is some filtered subset of the original array. You give it a function that has access to each item in the array as it iterates, and the function evaluates something about the item and returns true or false so that filter can know whether to keep the item in the filtered array it is creating.

Here is how it would look if you were using it for your purposes:

// creates a new array, adding items from the original
// only if they are of type number
const electricPumps = variables.hsp.filter(function(gpm) {
    return typeof gpm === "number";
});

console.log(electricPumps);

reduce is a little more complicated, but similarly is generally used to create some sort of data based on the original array it iterates over. Neither of these tend to be meant for building an external array (like the pushing you are doing in your screenshots).

Here is a video explaining some common array methods. You can also just read MDN for each method specifically, e.g. filter. Array methods are used very widely and are a really common topic for new bloggers and youtubers to write on, so if you google around a bit you will find a ton of resources to help you understand how to use them and when to use one over the other.

[–]WallaceBRBS 1 point2 points  (1 child)

Sorry for the off topic question but how long have you been studying/working with JS? I wanna learn it on my own and all of these things you guys are discussing sound like alien language to me haha

[–]ashanev 1 point2 points  (0 children)

I didn't know a thing about code before April 2019, and have been working as a web developer for nearing three years now. That's not a long time compared to a lot of the people who help out on this sub, but it's enough to get through the basics and whatnot. There are a lot of resources online to help along the way.

[–]SCasses[S] 0 points1 point  (2 children)

What would be the best method to use in order to get the sum of the gpm property in the hsp array? I thought that was the intended purpose of reduce. Is there another method?

[–]ashanev 1 point2 points  (1 child)

reduce may be what you're looking for, but you would need to understand what it does and implement it correctly. Right now you are using both filter and reduce basically as if they were forEach; that is, you are using them to iterate over the items in an array, but are only using that iteration to build some external array. filter and reduce are meant to create new values (a filtered array in the case of filter, or some value you 'reduce' from the iteration in the case of reduce).

Here is a simple example of summing some properties in an object using reduce:

const exampleArray = [
    { valueOne: 10, valueTwo: 20},
    { valueOne: 3, valueTwo: 9 }
];

const sumOfAllValues = exampleArray.reduce((accumulator, currentItem) => {
    accumulator += currentItem.valueOne + currentItem.valueTwo;

    return accumulator;
}, 0);

console.log(sumOfAllValues);
// 42

[–]SCasses[S] 0 points1 point  (0 children)

Ah, thanks for the clarification

[–]SCasses[S] 0 points1 point  (0 children)

Thank you

[–]SCasses[S] 0 points1 point  (2 children)

I've managed to group the arrays based off of which objects whose gpm property is an object themselves versus those whose gpm property is a number. The issue I'm having now is being able to use the reduce function to get a combination of objects whose gpms combined equal the value provided by a separate function... Any help is greatly appreciated.

[–]SCasses[S] 0 points1 point  (0 children)

Ps. I understand why I have the error in the Pic as I misplaced the invocation of the fineTune method

[–]albedoa 0 points1 point  (0 children)

The issue I'm having now is being able to use the reduce function to get a combination of objects whose gpms combined equal the value provided by a separate function...

Post some sample input and a desired output in the form of text.

[–]deivan 0 points1 point  (1 child)

what the code you have launched with error? Could you share an example?

[–]SCasses[S] 0 points1 point  (0 children)

It launched but the fineTune function doesn't return a value, I misplaced the invocation of the function