Running reduce() on jQuery collectionIs there an “exists” function for jQuery?Add table row in jQueryHow do I check if an element is hidden in jQuery?Setting “checked” for a checkbox with jQuery?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?Disable/enable an input with jQuery?How can I refresh a page with jQuery?jQuery scroll to element“Thinking in AngularJS” if I have a jQuery background?
What are the purposes of autoencoders?
Is this toilet slogan correct usage of the English language?
What is this called? Old film camera viewer?
How do I color the graph in datavisualization?
2.8 Why are collections grayed out? How can I open them?
Why can Carol Danvers change her suit colours in the first place?
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
On a tidally locked planet, would time be quantized?
Lowest total scrabble score
Is it improper etiquette to ask your opponent what his/her rating is before the game?
C++ debug/print custom type with GDB : the case of nlohmann json library
copy and scale one figure (wheel)
How to indicate a cut out for a product window
The IT department bottlenecks progress. How should I handle this?
Should I outline or discovery write my stories?
What was this official D&D 3.5e Lovecraft-flavored rulebook?
How to explain what's wrong with this application of the chain rule?
What was the exact wording from Ivanhoe of this advice on how to free yourself from slavery?
Why electric field inside a cavity of a non-conducting sphere not zero?
Did arcade monitors have same pixel aspect ratio as TV sets?
Is it safe to use olive oil to clean the ear wax?
Drawing ramified coverings with tikz
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
Why is it that I can sometimes guess the next note?
Running reduce() on jQuery collection
Is there an “exists” function for jQuery?Add table row in jQueryHow do I check if an element is hidden in jQuery?Setting “checked” for a checkbox with jQuery?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?Disable/enable an input with jQuery?How can I refresh a page with jQuery?jQuery scroll to element“Thinking in AngularJS” if I have a jQuery background?
I'm working on an application and I have a collection of DOM objects where I want to sum their combined widths. I wanted to use the reduce()
function for this.
The variable containing the jQuery collection is called menuItems
and my attempt looks like this:
menuItems.toArray().reduce((total, item) => total += item.clientWidth);
But weirdly, that function returns this output:
// "http://localhost:3000/97689982"
Which is equivalent to all of the widths concatenated together in a string excluding the first element.
Running something like menuItems.toArray()[0].clientWidth
correctly outputs the width of the first item.
jquery ecmascript-6
add a comment |
I'm working on an application and I have a collection of DOM objects where I want to sum their combined widths. I wanted to use the reduce()
function for this.
The variable containing the jQuery collection is called menuItems
and my attempt looks like this:
menuItems.toArray().reduce((total, item) => total += item.clientWidth);
But weirdly, that function returns this output:
// "http://localhost:3000/97689982"
Which is equivalent to all of the widths concatenated together in a string excluding the first element.
Running something like menuItems.toArray()[0].clientWidth
correctly outputs the width of the first item.
jquery ecmascript-6
What is the input data insidemenuItems
? can you post it here too?
– varit05
2 days ago
add a comment |
I'm working on an application and I have a collection of DOM objects where I want to sum their combined widths. I wanted to use the reduce()
function for this.
The variable containing the jQuery collection is called menuItems
and my attempt looks like this:
menuItems.toArray().reduce((total, item) => total += item.clientWidth);
But weirdly, that function returns this output:
// "http://localhost:3000/97689982"
Which is equivalent to all of the widths concatenated together in a string excluding the first element.
Running something like menuItems.toArray()[0].clientWidth
correctly outputs the width of the first item.
jquery ecmascript-6
I'm working on an application and I have a collection of DOM objects where I want to sum their combined widths. I wanted to use the reduce()
function for this.
The variable containing the jQuery collection is called menuItems
and my attempt looks like this:
menuItems.toArray().reduce((total, item) => total += item.clientWidth);
But weirdly, that function returns this output:
// "http://localhost:3000/97689982"
Which is equivalent to all of the widths concatenated together in a string excluding the first element.
Running something like menuItems.toArray()[0].clientWidth
correctly outputs the width of the first item.
jquery ecmascript-6
jquery ecmascript-6
edited 2 days ago


georgeawg
34.1k115370
34.1k115370
asked 2 days ago
funkylaundryfunkylaundry
78121123
78121123
What is the input data insidemenuItems
? can you post it here too?
– varit05
2 days ago
add a comment |
What is the input data insidemenuItems
? can you post it here too?
– varit05
2 days ago
What is the input data inside
menuItems
? can you post it here too?– varit05
2 days ago
What is the input data inside
menuItems
? can you post it here too?– varit05
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
You need to parse the client width from string to a number before reducing
it.
const totalClientWidth = menuItems.reduce((acc, cur) => acc + Number(cur.clientWidth), 0);
1
@TylerRoper fixed it.
– Kunal Mukherjee
2 days ago
1
Nice, thanks. Good answer :)
– Tyler Roper
2 days ago
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55281442%2frunning-reduce-on-jquery-collection%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
You need to parse the client width from string to a number before reducing
it.
const totalClientWidth = menuItems.reduce((acc, cur) => acc + Number(cur.clientWidth), 0);
1
@TylerRoper fixed it.
– Kunal Mukherjee
2 days ago
1
Nice, thanks. Good answer :)
– Tyler Roper
2 days ago
add a comment |
You need to parse the client width from string to a number before reducing
it.
const totalClientWidth = menuItems.reduce((acc, cur) => acc + Number(cur.clientWidth), 0);
1
@TylerRoper fixed it.
– Kunal Mukherjee
2 days ago
1
Nice, thanks. Good answer :)
– Tyler Roper
2 days ago
add a comment |
You need to parse the client width from string to a number before reducing
it.
const totalClientWidth = menuItems.reduce((acc, cur) => acc + Number(cur.clientWidth), 0);
You need to parse the client width from string to a number before reducing
it.
const totalClientWidth = menuItems.reduce((acc, cur) => acc + Number(cur.clientWidth), 0);
edited 2 days ago
answered 2 days ago


Kunal MukherjeeKunal Mukherjee
1,80121026
1,80121026
1
@TylerRoper fixed it.
– Kunal Mukherjee
2 days ago
1
Nice, thanks. Good answer :)
– Tyler Roper
2 days ago
add a comment |
1
@TylerRoper fixed it.
– Kunal Mukherjee
2 days ago
1
Nice, thanks. Good answer :)
– Tyler Roper
2 days ago
1
1
@TylerRoper fixed it.
– Kunal Mukherjee
2 days ago
@TylerRoper fixed it.
– Kunal Mukherjee
2 days ago
1
1
Nice, thanks. Good answer :)
– Tyler Roper
2 days ago
Nice, thanks. Good answer :)
– Tyler Roper
2 days ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55281442%2frunning-reduce-on-jquery-collection%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
What is the input data inside
menuItems
? can you post it here too?– varit05
2 days ago