Javascript: Get previous array element value?
-
I'm trying to do this col[i] = childWidth + col[i-1]; To get the previous array item value, but the output I get for this just says NaN. It works fine when I don't have the -1 in the array brackets. How do I get the previous value?
-
Answer:
It's hard to say unless you post previous lines but my guess would be childWidth is a number and col[i-1] is undefined. are all elements of your array col defined? Edit: OK, your loop is not posted fully, but assuming you're doing it correctly for(var i = 1; i < some_number; i++) and the line posted in your original post was inside the loop, childWidth should be undefined. Make sure you've defined it somewhere before col[i] = childWidth + col[i-1]; possibly before the loop.
David at Yahoo! Answers Visit the source
Related Q & A:
- How to pass javascript jQuery variable value in php array?Best solution by Stack Overflow
- How to remove first element in multidimentional array?Best solution by Stack Overflow
- How can I remove duplicate Objects from array of Objects in javascript?Best solution by Stack Overflow
- How to get Javascript array length?Best solution by Stack Overflow
- How to dynamically create object properties from an array in Javascript?Best solution by nfriedly.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.