Andrea Salvadori
Andrea Salvadori

Reputation: 11

Javascript array returns only single character

Okay, this is my first question on stack overflow, i hope to do it correctly. I'm actually working on a function that takes the values of some var that i have saved in an array in a cookie. As you can see i tried like, anything i can but it doesn't work correctly. The var p1, for example if the array is [20,1000,1,0,0], returns the number 2 instead of 20. It's like it doesn't treat the var stringfy as an array. Sorry for the mess, hope you can help.

function getCookie() {
"use strict";
var value = "; " + document.cookie;
alert (value);              // this returns ; ciccio=[20,1000,1,0,0]
var ciccio = "ciccio";      // the name of the cookie to split it  
var parts = value.split("; " +ciccio+ "=");
var arr = parts.pop().split(";").shift();
alert (arr);                // this returns [20,1000,1,0,0]
var arr1 = arr.slice(1, -1);
alert(arr1);                // this returns 20,1000,1,0,0
var stringfy = JSON.stringify(arr); 
alert (stringfy);           // this returns "20,1000,1,0,0
var p1 = parseInt(stringfy[1]);
alert (p1);                 // this returns 2
p = p1;
document.getElementById("p").innerHTML = p;

}

P.s it doesn't work even without the json stringfy or without the arr.slice, i was just trying all that came in my mind.

Upvotes: 0

Views: 1002

Answers (4)

Scott Marcus
Scott Marcus

Reputation: 65835

You are not getting what you think you are getting. You have a string from the start and, after that, when you call JSON.stringify(), you are doing that to a string, which gives you a second set of quotes in that string. Then when you use stringify[1], you are attempting to get the second character from that string.

See comments in code below for actual values you have.

var value = "; ciccio=[20,1000,1,0,0]"; // simulated cookie value
console.log(value);
var ciccio = "ciccio";      // the name of the cookie to split it  
var parts = value.split("; " + ciccio + "=");

// No need for pop and shift. Just drop the first part and use the second
var arr = parts[1];

console.log(arr);                // this returns "[20,1000,1,0,0]" <-- STRING
var arr1 = arr.slice(1, -1);
console.log(arr1);                // this returns "20,1000,1,0,0" <-- STRING

// You still have an array. This is what you should be passing your
// index to in order to get parts of the cookie data out:
console.log(arr1[1]);

// This will take your string and stringify it again!
var stringfy = JSON.stringify(arr); 
console.log(stringfy);           // this returns '"[20,1000,1,0,0]"' <-- STRING with quotes in it

// This line converts the second character in the string to an integer and returns it
var p1 = parseInt(stringfy[1], 10);
console.log(p1);                 // this returns NaN, not 2!

Getting the cookie string as an array is much easier than you are doing:

var value = "; ciccio=[20,1000,1,0,0]"; // simulated cookie value
console.log(value);
 
// Create an array of 2 elements: ["; ciccio=", "[20,1000,1,0,0]"]
var parts = value.split("; ciccio=");

// Turn second array element (a string) into array of its own:
var ary = JSON.parse(parts[1]);

// Access array as normal
console.log(ary[1]);

Upvotes: 2

George
George

Reputation: 6739

The problem is with your array in the fact that it never actually is an array. It's always a string, even before you JSON.stringify() it.

You can convert it to a actual array at the point where your have the value arr with the use of JSON.parse()

var value = "; " + "ciccio=[20,1000,1,0,0]"
console.log(value);
var ciccio = "ciccio"; // the name of the cookie to split it  
var parts = value.split("; " + ciccio + "=");
var arr = parts.pop().split(";").shift();
console.log(arr)
var arr1 = JSON.parse(arr);
console.log(arr1[1])

Upvotes: 1

atomrc
atomrc

Reputation: 2583

document.cookie is a string, so you need to parse it in order to convert it to array.

If you want the ciccio value, you can use regExp like this:

var ciccioMatches = document.cookie.match(/ciccio=([^;]+);/)

console.log(ciccioMatches); // ['ciccio=[....]', '[....]']

The second result will be the serialized array, so you need to JSON.parse it to get it as an actual array:

var ciccio = JSON.parse(ciccionMatches[1]);

No need to say that you need to check that there are some matches before actually parsing the first index of the ciccionMatches array

Hope it helps

Upvotes: 0

Emad Dehnavi
Emad Dehnavi

Reputation: 3451

Here you go :

let obj ="[20,1000,1,0,0]";
let newObj = JSON.parse(obj);

console.log(newObj[0]); //return 20 

https://jsfiddle.net/emilvr/a1uzjf3o/

Upvotes: -1

Related Questions