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
})
javascript
|
show 4 more comments
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
})
javascript
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? Ifdata.sort
doesn't throw an error due todata
being undefined, then the function should return something that isn'tundefined
– CertainPerformance
Nov 10 at 21:42
Shouldn'ta["percentage.funded"]
bea.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 generatestestData
) is not the same function assortPercentHighestOrLeast
(the function you posted in the second code block). What issortAmountPledgingMaximumOrMinimum
?
– CertainPerformance
Nov 10 at 21:47
1
Then the problem is insortAmountPledgingMaximumOrMinimum()
– 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
|
show 4 more comments
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
})
javascript
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
javascript
edited Nov 10 at 21:50
asked Nov 10 at 21:41
KuchBhi
574218
574218
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? Ifdata.sort
doesn't throw an error due todata
being undefined, then the function should return something that isn'tundefined
– CertainPerformance
Nov 10 at 21:42
Shouldn'ta["percentage.funded"]
bea.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 generatestestData
) is not the same function assortPercentHighestOrLeast
(the function you posted in the second code block). What issortAmountPledgingMaximumOrMinimum
?
– CertainPerformance
Nov 10 at 21:47
1
Then the problem is insortAmountPledgingMaximumOrMinimum()
– 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
|
show 4 more comments
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? Ifdata.sort
doesn't throw an error due todata
being undefined, then the function should return something that isn'tundefined
– CertainPerformance
Nov 10 at 21:42
Shouldn'ta["percentage.funded"]
bea.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 generatestestData
) is not the same function assortPercentHighestOrLeast
(the function you posted in the second code block). What issortAmountPledgingMaximumOrMinimum
?
– CertainPerformance
Nov 10 at 21:47
1
Then the problem is insortAmountPledgingMaximumOrMinimum()
– 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
|
show 4 more comments
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
);
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 doingconsole.log(testData)
it was coming out to be undefined. @CertainPerformance pointed out my mistake in comment section.
– KuchBhi
Nov 10 at 22:19
add a comment |
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
);
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 doingconsole.log(testData)
it was coming out to be undefined. @CertainPerformance pointed out my mistake in comment section.
– KuchBhi
Nov 10 at 22:19
add a comment |
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
);
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 doingconsole.log(testData)
it was coming out to be undefined. @CertainPerformance pointed out my mistake in comment section.
– KuchBhi
Nov 10 at 22:19
add a comment |
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
);
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
);
answered Nov 10 at 21:58
Nina Scholz
168k1382145
168k1382145
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 doingconsole.log(testData)
it was coming out to be undefined. @CertainPerformance pointed out my mistake in comment section.
– KuchBhi
Nov 10 at 22:19
add a comment |
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 doingconsole.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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 todata
being undefined, then the function should return something that isn'tundefined
– CertainPerformance
Nov 10 at 21:42
Shouldn't
a["percentage.funded"]
bea.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 generatestestData
) is not the same function assortPercentHighestOrLeast
(the function you posted in the second code block). What issortAmountPledgingMaximumOrMinimum
?– 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