简体   繁体   English

为什么我的字符串没有在 jQuery 中转换为数组

[英]Why isn't my string being converted into an Array in jQuery

The purpose of the code below is to get an array to be used with the Google Map API to display multiple location markers on a Google Map.下面代码的目的是获取一个数组,与 Google Map API 一起使用,以在 Google 地图上显示多个位置标记。 I get my json array from a php file through an Ajax response.我通过 Ajax 响应从 php 文件中获取我的 json 数组。 When I stringify my json response it looks like this:当我将 json 响应字符串化时,它看起来像这样:

[["Sondrevegen 8 - Oppføring av tomannsbolig, SONDREVEGEN 8, Oslo"],["Sondrevegen 6 - Oppføring av 
enebolig, SONDREVEGEN 6, Oslo"],["Skådalsveien 2 D - Oppføring av enebolig, SKÅDALSVEIEN 2 D, Oslo"], 
["Rosendalsveien 23 - Oppføring av enebolig - Hus 1, ROSENDALSVEIEN 23, Oslo"],["Skådalsveien 10 E - 
Oppføring av enebolig - Hus 4, SKÅDALSVEIEN 10 E, Oslo"]]

The format I need in the Array to be able to execute the Google Map API call without errors is this:我需要在 Array 中执行 Google Map API 调用而不会出错的格式是这样的:

[["Sondrevegen 8 - Oppføring av tomannsbolig", "SONDREVEGEN 8", "Oslo"],
["Sondrevegen 6 - Oppføring av enebolig", "SONDREVEGEN 6", "Oslo"],
["Skådalsveien 2 D - Oppføring av enebolig", "SKÅDALSVEIEN 2 D", "Oslo"],
["Rosendalsveien 23 - Oppføring av enebolig - Hus 1", "ROSENDALSVEIEN 23", "Oslo"],
["Skådalsveien 10 E - Oppføring av enebolig - Hus 4", "SKÅDALSVEIEN 10 E", "Oslo"]]

As you can see I am missing double quotes enclosing the values in the Array.如您所见,我缺少将数组中的值括起来的双引号。 My research tells me that I need to convert the array into a string to add the double quotes to the values in the string, and then convert the string back onto an Array like this:我的研究告诉我,我需要将数组转换为字符串以将双引号添加到字符串中的值,然后将字符串转换回这样的数组:

var test = response;
var eventlist;
var eventstring = new String();

for (var i = 0, len = test.length; i < len; i++) {
    content = '['+test[i]+']'
    eventlist = eventlist + content;
    }
eventstring = eventlist.toString().replace(/"/g, "");    
let arr = eventstring.split(',');

The code above returns the following output in the console log.上面的代码在控制台日志中返回以下输出。

["undefined[Sondrevegen 8 - Oppføring av tomannsbolig", " SONDREVEGEN 8", " Oslo][Sondrevegen 6 - 
Oppføring av enebolig", " SONDREVEGEN 6", " Oslo][Skådalsveien 2 D - Oppføring av enebolig", " 
SKÅDALSVEIEN 2 D", " Oslo][Rosendalsveien 23 - Oppføring av enebolig - Hus 1", " ROSENDALSVEIEN 23", 
" Oslo][Skådalsveien 10 E - Oppføring av enebolig - Hus 4", " SKÅDALSVEIEN 10 E", " Oslo]"]

The output above starts with a double quote and the value 'undefined'.上面的输出以双引号和值 'undefined' 开头。 I believe my output isn't an array at all, but I am not able to sort this out.我相信我的输出根本不是数组,但我无法解决这个问题。 Any pointer in the right direction is highly appreciated任何指向正确方向的指针都受到高度赞赏

 var array = [["Sondrevegen 8 - Oppføring av tomannsbolig, SONDREVEGEN 8, Oslo"],["Sondrevegen 6 - Oppføring avenebolig, SONDREVEGEN 6, Oslo"],["Skådalsveien 2 D - Oppføring av enebolig, SKÅDALSVEIEN 2 D, Oslo"], ["Rosendalsveien 23 - Oppføring av enebolig - Hus 1, ROSENDALSVEIEN 23, Oslo"],["Skådalsveien 10 E - Oppføring av enebolig - Hus 4, SKÅDALSVEIEN 10 E, Oslo"]]; for(var i=0; i< array.length; i++) { array[i] = array[i][0].split(', '); } console.log(array)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM