Javascript数组只返回单个字符

时间:2017-05-22 12:16:28

标签: javascript json

好的,这是关于堆栈溢出的第一个问题,我希望能正确完成。 我实际上正在开发一个函数,它接受我在cookie中的数组中保存的一些var的值。 你可以看到我尝试过,我能做的任何东西,但它无法正常工作。 var p1,例如,如果数组是[20,1000,1,0,0],则返回数字2而不是20。 这就像它不会将var stringfy视为一个数组。 对不起,请帮忙。

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;

}

即使没有json stringfy或没有arr.slice,它也不起作用,我只是在尝试所有这一切都在我脑海中。

4 个答案:

答案 0 :(得分: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!
&#13;
&#13;
&#13;

将cookie字符串作为数组获取比执行起来容易得多:

&#13;
&#13;
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]);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

问题在于你的数组,因为它实际上从来就不是一个数组。它始终是一个字符串,甚至在你JSON.stringify()之前。

您可以使用arr

将其转换为具有值JSON.parse()实际数组

&#13;
&#13;
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])
&#13;
&#13;
&#13;

答案 2 :(得分:0)

document.cookie是一个字符串,因此您需要解析它以将其转换为数组。

如果你想要ciccio值,你可以像这样使用regExp:

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

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

第二个结果将是序列化数组,因此您需要JSON.parse将其作为实际数组获取:

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

在实际解析ciccionMatches数组的第一个索引

之前,无需说您需要检查是否存在一些匹配项

希望有所帮助

答案 3 :(得分:-1)

你走了:

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

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

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