0

I got a problem in how an array is formatted, here is my example :

var code = 743919183;
var tempPost = '["'+code+'"]';
$scope.arrayTwo = [];
$scope.arrayTwo.push(code);

when in console :

tempPost will output like this : ["743919183"]

while $scope.arrayTwo, have space before and after doublequotes [ "743919183" ]

I want to have $scope.ArrayTwo array is formatted just like tempPost, how do i do that?

1
  • 2
    Why do you want to do that? What's the purpose? Commented Sep 3, 2014 at 10:37

3 Answers 3

2

I'm not sure what you are trying to achieve but:

var code = 743919183; //This is an integer to convert to string just call .toString()
var tempPost = '["'+code+'"]'; //This is a string that has a value equal to "["743919183"]", this is not an array.
$scope.arrayTwo = [] //This is a an empty array;
$scope.arrayTwo.push(code); //This will add a new item to the array in this case the integer code. Your array would look like [743919183].

Now, if what you want is an array that has one item that is code as a string you should do:

var code = "743919183";
$scope.arrayTwo = [code];

Or:

var code = 743919183;
$scope.arrayTwo = [code.toString()];
Sign up to request clarification or add additional context in comments.

Comments

0
var code = 743919183;
var tempPost = '["'+code+'"]';
$scope.arrayTwo = [];
$scope.arrayTwo.push(code);

in firefox $scope.arrayTwo return [ "743919183" ] and chrome ["743919183"](no space in chrome). so i think this should not be create problem in code.

1 Comment

it will be a problem when i try to grab the data using $_PHP['products'] for example.... the $scope.arrayTwo is in false format....but when i try to post tempPost , it work
0

If you just want it to look like array format without being an actual array you can do this:

var code = 743919183;
var tempPost = '["'+code+'"]';
$scope.arrayTwo = [];
$scope.arrayTwo.push(code);

Is that what you are going for?

OR you can do this

var code = 743919183;
$scope.arrayTwo = [];

function Str2Arr() {

  s = '';

  for (var i=0; i<code.length; i++) {

    if (code[i] != ',') { s += code[i]; }

                  else { arrayTwo .push(s);  s = ''; }

  } 

  arrayTwo .push(s);

  alert(arrayTwo .join('\n'));

}

Str2Arr();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.