Solved

JavaScript - string to an array

Posted on 2016-09-29
4
31 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 54

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

786 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