Solved

JavaScript - string to an array

Posted on 2016-09-29
4
20 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 32

Accepted Solution

by:
Stefan Hoffmann earned 250 total points (awarded by participants)
Comment Utility
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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points (awarded by participants)
Comment Utility
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 32

Assisted Solution

by:Stefan Hoffmann
Stefan Hoffmann earned 250 total points (awarded by participants)
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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. …
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

744 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

8 Experts available now in Live!

Get 1:1 Help Now