将字符串数组值转换为数组对象 [英] Turn String array values into array object

查看:83
本文介绍了将字符串数组值转换为数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个字符串传递到页面上的一个隐藏字段中,然后将其分开,以尝试获取可迭代的项目数组,并将多个标记添加到Google地图上。如果我硬编码数组,它的工作正常,但如果我尝试拆分字符串并使用相同的代码,它不起作用。我假设,因为分割字符串创建一个字符串数组而不是数组对象。当我分割字符串时,数组以['Smith Company',33.61678,-111.90017]结尾,并用双引号括起来。我将如何转换这些项目,以便我可以像硬编码值一样使用它?

我在代码后面的StringBuilder中添加方括号,单引号和垂直条。使用C#,如果它会有所作为。我没有这样做,但仍然绿色与JavaScript,所以这是我最初的解决方案。我的初始字符串看起来像这样

 ['Jones Company',26.16683,-98.23342] | ['Smith Company', ['Red Company',42.46692,-114.48342]


$ b $ p
$ p
$ b $ p $ function initialize( ){
var myOptions = {
zoom:4,
center:new google.maps.LatLng(26.16683,-98.23342),
mapTypeId:google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById(map_canvas),myOptions);

//我尝试的当硬编码被注释掉时使用
var myString = document.getElementById('hdnString')。value;
var myItems = new Array(); $ b $ myItems = myString.split(| );

//尝试上述
v时我注释掉的硬编码项目ar myItems = [
['Jones Company',26.16683,-98.23342],
['Smith Company',33.61678,-111.90017],
['Bob Company',37.71675,-122.21672 ],
['Blue Company',42.46692,-114.48342],
['Red Company',36.58339,-121.8335]
];

//将标记添加到地图
(myItems中的var i){
var myLatLng = new google.maps.LatLng(myItems [i] [1],myItems [I] [2]);
var marker = new google.maps.Marker({
position:myLatLng,
map:map,
title:myItems [i] [0]
}) ;



$ div $解析方案

你将不得不使用 eval()将数组的字符串表示转换为实际的javascript数组。



这段代码应该可以工作:

  var myString = document。 。的getElementById( 'hdnString')值; 
var myItems = [];
var rawItems = myString.split(|); $ var $ i

for(var i = 0; i< rawItems.length; i ++){
myItems.push(eval(rawItems [i]));
}

请注意 eval是邪恶的当它不是时。


I am passing a string into a hidden field on the page and then splitting it in order to try to get an array of items I can iterate over and add multiple markers onto a google map. If I hard code the array it works fine, but if I try to split the string and use the same code it does not work. I'm assuming because splitting the string creates a string array instead of Array objects. When I split the string the array ends up with "['Smith Company', 33.61678, -111.90017]" with the double quotes around it. How would I convert these items so that I can use it the same as the hard coded values?

I add the square brackets, single quotes and vert bar in a StringBuilder in the code behind. Using C# for that if it will make a difference. I am not married to doing it this way, but still green with JavaScript so this was my initial solution. My initial string looks like this"

"['Jones Company', 26.16683, -98.23342]|['Smith Company', 33.61678, -111.90017]|['Bob Company', 29.70008, -98.03347]|['Blue Company', 37.71675, -122.21672]|['Red Company', 42.46692, -114.48342]"

Thanks.

function initialize() {
        var myOptions = {
            zoom: 4,
            center: new google.maps.LatLng(26.16683, -98.23342),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        //What I try to use when the hard coded is commented out
        var myString = document.getElementById('hdnString').value;
        var myItems = new Array();
        myItems = myString.split("|");

        //Hard coded items that I comment out when trying the above
        var myItems = [
            ['Jones Company', 26.16683, -98.23342],
            ['Smith Company', 33.61678, -111.90017],
            ['Bob Company', 37.71675, -122.21672],
            ['Blue Company', 42.46692, -114.48342],
            ['Red Company', 36.58339, -121.8335]
        ];

            // Add markers to the map
            for (var i in myItems){
                var myLatLng = new google.maps.LatLng(myItems[i][1], myItems[i][2]);
                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    title: myItems[i][0]
                });
            }
        }

解决方案

You're going to have to use eval() to convert the string representations of arrays to actual javascript arrays.

This code ought to work:

var myString = document.getElementById('hdnString').value;
var myItems = [];
var rawItems = myString.split("|");

for (var i=0; i<rawItems.length; i++){
    myItems.push(eval(rawItems[i]));
}

Do note that eval is evil except when it's not.

这篇关于将字符串数组值转换为数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆