Home > Back-end >  how can I split a string into an array with js
how can I split a string into an array with js

Time:11-27

i have a problem why dispense doesn't get the amount of data that has been calculated by the loop.

why the demo2 doesn't count all the data totals and how to calculate the amount of data in the demo2?

const demo1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

const demo2 = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15";
const array_demo2 = [demo2];

let text1 = "";
let number1 = "";
for (let i = 0; i < demo1.length; i  ) {
  number1 = (i   1);
  text1  = demo1[i];
}

let text2 = "";
let number2 = "";
for (let i = 0; i < array_demo2.length; i  ) {
  number2 = (i   1);
  text2  = array_demo2[i];
}

document.getElementById("demo1").innerHTML = number1  ' = '  text1;
document.getElementById("demo2").innerHTML = number2  ' = '  text2;
<div style="display: flex"><p>Output 1 =></p>&nbsp;<p id="demo1"></p></div>
<div style="display: flex"><p>Output 2 =></p>&nbsp;<p id="demo2"></p></div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

demo1 is an array, meaning multiple variables of the same type inside a container (the array is the container)

demo2 is a single variable, a string. you can

demo2.split(','); // to get an array like demo1, but it will be an array of strings.

array_demo2 is an array with a single element, the demo2 string. You can

array_demo2[0].split(','); // to get an array like demo2/demo1

Read more about arrays here The text is for C/ but in Javascript arrays are very similiar.

CodePudding user response:

const demo2 = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15";
const array_demo2 = [demo2];

this is not the correct way to convert it to number you're just passing a string in an array which will be on it's first index, so this array will have only 1 length.

its same like if you do lets say:

let fruit = "apple"
let fruits = [fruit];

fruits array will always have only one fruit on its first index. so the correct way is to use the .split method of array, so in your case it would be something

const demo2 = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15";
const array_demo2 = demo2.split(",");

now it will split each number seprated by "," and add it to new index on the array.

CodePudding user response:

default value assigned to variable is string, as a result string concatenation is happening, assigned that to number 0.

array_demo2 is having one string item "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15", not the array of numbers you are thinking. You can do that using split() and map():

const array_demo2 = demo2.split(',').map(Number);

Demo:

const demo1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

const demo2 = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15";
const array_demo2 = demo2.split(',').map(Number);

let text1 = 0;
let number1 = 0;
for (let i = 0; i < demo1.length; i  ) {
  number1 = (i   1);
  text1  = demo1[i];
}

let text2 = 0;
let number2 = 0;
for (let i = 0; i < array_demo2.length; i  ) {
  number2 = (i   1);
  text2  = array_demo2[i];
}

document.getElementById("demo1").innerHTML = number1  ' = '  text1;
document.getElementById("demo2").innerHTML = number2  ' = '  text2;
<div style="display: flex"><p>Output 1 =></p>&nbsp;<p id="demo1"></p></div>
<div style="display: flex"><p>Output 2 =></p>&nbsp;<p id="demo2"></p></div>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related