How to write code in functional way in Js











up vote
1
down vote

favorite












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









share|improve this question




















  • 1




    Looks odd, are you sure that's the exact code you're using? Can you post a live Minimal, Complete, and Verifiable 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
    – CertainPerformance
    Nov 10 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.
    – ibrahim mahrir
    Nov 10 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?
    – CertainPerformance
    Nov 10 at 21:47








  • 1




    Then the problem is in sortAmountPledgingMaximumOrMinimum()
    – Nikko Khresna
    Nov 10 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".
    – Andy
    Nov 10 at 21:59















up vote
1
down vote

favorite












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









share|improve this question




















  • 1




    Looks odd, are you sure that's the exact code you're using? Can you post a live Minimal, Complete, and Verifiable 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
    – CertainPerformance
    Nov 10 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.
    – ibrahim mahrir
    Nov 10 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?
    – CertainPerformance
    Nov 10 at 21:47








  • 1




    Then the problem is in sortAmountPledgingMaximumOrMinimum()
    – Nikko Khresna
    Nov 10 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".
    – Andy
    Nov 10 at 21:59













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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









share|improve this question















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






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 21:50

























asked Nov 10 at 21:41









KuchBhi

621218




621218








  • 1




    Looks odd, are you sure that's the exact code you're using? Can you post a live Minimal, Complete, and Verifiable 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
    – CertainPerformance
    Nov 10 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.
    – ibrahim mahrir
    Nov 10 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?
    – CertainPerformance
    Nov 10 at 21:47








  • 1




    Then the problem is in sortAmountPledgingMaximumOrMinimum()
    – Nikko Khresna
    Nov 10 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".
    – Andy
    Nov 10 at 21:59














  • 1




    Looks odd, are you sure that's the exact code you're using? Can you post a live Minimal, Complete, and Verifiable 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
    – CertainPerformance
    Nov 10 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.
    – ibrahim mahrir
    Nov 10 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?
    – CertainPerformance
    Nov 10 at 21:47








  • 1




    Then the problem is in sortAmountPledgingMaximumOrMinimum()
    – Nikko Khresna
    Nov 10 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".
    – Andy
    Nov 10 at 21:59








1




1




Looks odd, are you sure that's the exact code you're using? Can you post a live Minimal, Complete, and Verifiable 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
– CertainPerformance
Nov 10 at 21:42






Looks odd, are you sure that's the exact code you're using? Can you post a live Minimal, Complete, and Verifiable 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
– CertainPerformance
Nov 10 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.
– ibrahim mahrir
Nov 10 at 21:46






Shouldn't a["percentage.funded"] be a.percentage.funded? Are you sure the key is "percentage.funded"? BTW, sort mutates the original array.
– ibrahim mahrir
Nov 10 at 21:46






3




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?
– CertainPerformance
Nov 10 at 21:47






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?
– CertainPerformance
Nov 10 at 21:47






1




1




Then the problem is in sortAmountPledgingMaximumOrMinimum()
– Nikko Khresna
Nov 10 at 21:52




Then the problem is in sortAmountPledgingMaximumOrMinimum()
– Nikko Khresna
Nov 10 at 21:52




1




1




I don't there is a problem any more @CertainPerformance as it was the wrong function name... The OP said "That fixed it".
– Andy
Nov 10 at 21:59




I don't there is a problem any more @CertainPerformance as it was the wrong function name... The OP said "That fixed it".
– Andy
Nov 10 at 21:59












1 Answer
1






active

oldest

votes

















up vote
2
down vote













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





share|improve this answer





















  • are you sure it will work? For some reason i don't see any changes in my data
    – KuchBhi
    Nov 10 at 22:10










  • 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?
    – Nina Scholz
    Nov 10 at 22:12












  • 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.
    – KuchBhi
    Nov 10 at 22:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243689%2fhow-to-write-code-in-functional-way-in-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













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





share|improve this answer





















  • are you sure it will work? For some reason i don't see any changes in my data
    – KuchBhi
    Nov 10 at 22:10










  • 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?
    – Nina Scholz
    Nov 10 at 22:12












  • 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.
    – KuchBhi
    Nov 10 at 22:19















up vote
2
down vote













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





share|improve this answer





















  • are you sure it will work? For some reason i don't see any changes in my data
    – KuchBhi
    Nov 10 at 22:10










  • 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?
    – Nina Scholz
    Nov 10 at 22:12












  • 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.
    – KuchBhi
    Nov 10 at 22:19













up vote
2
down vote










up vote
2
down vote









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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 21:58









Nina Scholz

169k1382146




169k1382146












  • are you sure it will work? For some reason i don't see any changes in my data
    – KuchBhi
    Nov 10 at 22:10










  • 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?
    – Nina Scholz
    Nov 10 at 22:12












  • 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.
    – KuchBhi
    Nov 10 at 22:19


















  • are you sure it will work? For some reason i don't see any changes in my data
    – KuchBhi
    Nov 10 at 22:10










  • 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?
    – Nina Scholz
    Nov 10 at 22:12












  • 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.
    – KuchBhi
    Nov 10 at 22:19
















are you sure it will work? For some reason i don't see any changes in my data
– KuchBhi
Nov 10 at 22:10




are you sure it will work? For some reason i don't see any changes in my data
– KuchBhi
Nov 10 at 22:10












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?
– Nina Scholz
Nov 10 at 22:12






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?
– Nina Scholz
Nov 10 at 22:12














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.
– KuchBhi
Nov 10 at 22:19




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.
– KuchBhi
Nov 10 at 22:19


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243689%2fhow-to-write-code-in-functional-way-in-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation