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
})
data.sortdoesn't throw an error due todatabeing undefined, then the function should return something that isn'tundefineda["percentage.funded"]bea.percentage.funded? Are you sure the key is"percentage.funded"? BTW,sortmutates the original array..sortAmountPledgingMaximumOrMinimum(what generatestestData) is not the same function assortPercentHighestOrLeast(the function you posted in the second code block). What issortAmountPledgingMaximumOrMinimum?sortAmountPledgingMaximumOrMinimum()