Solved

JavaScript - string to an array

Posted on 2016-09-29
4
26 Views
Last Modified: 2016-10-19
Just need an example of how to copy a string of numbers to an array.  I have a number attribute (UPC) that can contain either 11 or 12 digits and I need to loop through each digital.  

Example; UPC  = 123456789012
                              1234457333
0
Comment
Question by:tmajor99
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 250 total points (awarded by participants)
ID: 41822248
Use .split():

var UPC = '1234457333'; 
var chars = UPC.split('');
for (var i = 0; i < chars.length; i++) {
    console.log('char at pos ' + i + ': ' + chars[i]);
}

Open in new window

1
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points (awarded by participants)
ID: 41822658
No need to split - this also works
var UPC = '1234457333';
for(var i = 0; i < UPC.length;i++) {
  console.log(UPC[i]);
}

Open in new window

0
 
LVL 33

Assisted Solution

by:ste5an
ste5an earned 250 total points (awarded by participants)
ID: 41823102
btw, the question is do you want the digits as number or string?

var UPC = '1234457333';
var chars = UPC.split('');
var digits = []
for (var i = 0; i < chars.length; i++) {
    console.log('char at pos ' + i + ': ' + chars[i] + ', data type: ' + typeof chars[i]);
    digits.push(parseInt(chars[i]));
}

for (var i = 0; i < digits.length; i++) {
    console.log('digits at pos ' + i + ': ' + digits[i] + ', data type: ' + typeof digits[i]);
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now