2017-05-22 56 views
-1

好的,這是我關於堆棧溢出的第一個問題,我希望能夠正確地做到這一點。 我實際上正在研究一個函數,該函數將一些var的值保存在cookie中的一個數組中。 正如你所看到的我嘗試過,我可以做任何事情,但它不能正常工作。 例如,如果數組爲[20,1000,1,0,0],則var p1返回數字2而不是20. 就像它不會將var stringfy視爲數組一樣。 對不起,我希望你能幫上忙。Javascript數組只返回單個字符

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它甚至不無JSON stringfy或不arr.slice工作,我只是想所有排在我的腦海裏。

+0

調用'JSON.parse(ARR)'然後你將有一個實際的數組 – George

+2

* Stringify *表示**變成一個字符串**,所以「是的,顯然這是一個字符串,你明確要求它是一個」。 – Quentin

回答

2

你沒有得到你認爲你得到的東西。從一開始你就有一個字符串,在此之後,當你撥打JSON.stringify()時,你就是這樣做了一個字符串,,這個字符串給你第二組引號。然後,當您使用stringify[1]時,您試圖從該字符串中獲取第二個字符。

請參閱下面代碼中的註釋以瞭解您的實際值。

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!

獲取cookie字符串作爲一個數組是容易得多比你正在做的:

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]);

+0

你在找什麼在p1你有arr1 [0] – Marco

+1

'arr1 ==「20,1000,1,0,0」'so'arr1 [1] ==「0」'他們沒有任何意義創建一個數組,它總是一個字符串。他們有一個有效的_array string_,其值爲'arr',所以他們可以執行'var arr1 = JSON.parse(arr);'然後'arr1 [1]'來獲得'1000' – George

0

document.cookie是一個字符串,所以你需要對它進行解析,以便將其轉換爲陣列。

如果你想ciccio值,你可以使用正則表達式是這樣的:

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

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

第二個結果將是序列化數組,所以你需要JSON.parse它把它作爲一個實際的數組:

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

沒有必要說,你需要檢查,有一些比賽之前實際解析ciccionMatches數組的第一個指數

希望它有幫助

1

問題是你的數組事實上,它從來沒有真正的數組。它總是一個字符串,甚至在你JSON.stringify()它之前。

您可以在點它轉換爲實際數組,其中你有值arr與使用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])