2

How do we write code in a functional manner inside Javascript?

So what I initially did was something like this

render ()  {

    if (!this.props.mainListFetching && !this.props.mainListError) {
      let testData = this.sortAmountPledgingMaximumOrMinimum(this.props.mainList, "maximum")
      console.log(testData) //Coming out to be undefined
        this.sortTypeTownOrCountry(this.props.mainList)
        this.sortPercentHighestOrLeast(this.props.mainList)

    }

this calls the following function

 sortPercentHighestOrLeast = (data, type) => {
       data.sort((a, b) => {
           if (type == "maximum") {
            return (
                a["percentage.funded"] - b["percentage.funded"]
            )
        } else {
            return (
                b["percentage.funded"] - a["percentage.funded"]
            )
        }
     }) 
     console.log(data)
     return data
}

I was expecting the result from the above function will be saved in my testData variable but when I console.log, it is coming out to be undefined.

My console.log(data) is printing result in console which confirms that function is executing and data isn't undefined?

[Update:] I have also tried this (since Js is async)

 let testData =  this.sortAmountPledgingMaximumOrMinimum(this.props.mainList, "maximum", (data) => {
            console.log("anything") //Doesn't log anything
          })
9
  • 1
    Looks odd, are you sure that's the exact code you're using? Can you post a live minimal reproducible example that illustrates the problem? If data.sort doesn't throw an error due to data being undefined, then the function should return something that isn't undefined Commented Nov 10, 2018 at 21:42
  • Shouldn't a["percentage.funded"] be a.percentage.funded? Are you sure the key is "percentage.funded"? BTW, sort mutates the original array. Commented Nov 10, 2018 at 21:46
  • 3
    Do note that .sortAmountPledgingMaximumOrMinimum (what generates testData) is not the same function as sortPercentHighestOrLeast (the function you posted in the second code block). What is sortAmountPledgingMaximumOrMinimum? Commented Nov 10, 2018 at 21:47
  • 1
    Then the problem is in sortAmountPledgingMaximumOrMinimum() Commented Nov 10, 2018 at 21:52
  • 1
    I don't there is a problem any more @CertainPerformance as it was the wrong function name... The OP said "That fixed it". Commented Nov 10, 2018 at 21:59

1 Answer 1

3

Assuming funded is a nested property of percentage, you could move the check of the condition a step ahead because you check for each sorting iteration.

sortPercentHighestOrLeast = (data, type) => data.sort(type => type === "maximum"
    ? (a, b) => a.percentage.funded - b.percentage.funded
    : (a, b) => b.percentage.funded - a.percentage.funded
);
Sign up to request clarification or add additional context in comments.

3 Comments

are you sure it will work? For some reason i don't see any changes in my data
you don't even add your data to the question, so i assume, it should work. btw, why do you assume to get an answer without supplying data of the array?
Sorry my bad and thanks for answering the question. btw - I think original question didn't actually require you to have data since console.log of the function was logging something but when I was doing console.log(testData) it was coming out to be undefined. @CertainPerformance pointed out my mistake in comment section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.