Solved

JavaScript - string to an array

Posted on 2016-09-29
4
42 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

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 57

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 34

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

759 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