Solved

JavaScript - string to an array

Posted on 2016-09-29
4
47 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 58

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

615 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