Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据追加到关联数组或维度数组_Javascript_Jquery_Arrays_Multidimensional Array_Associative Array - Fatal编程技术网

Javascript 将数据追加到关联数组或维度数组

Javascript 将数据追加到关联数组或维度数组,javascript,jquery,arrays,multidimensional-array,associative-array,Javascript,Jquery,Arrays,Multidimensional Array,Associative Array,我有一个国家选择,它填充城市选择, 然后在特定城市周围添加半径 var countrySet = new Array; var citySet = new Array; var radiusSet = new Array; var set; function appendCountry(){ if($.inArray($("#businessCountryTarget option:selecte

我有一个国家选择,它填充城市选择, 然后在特定城市周围添加半径

var countrySet = new Array;
var citySet = new Array;
var radiusSet = new Array;
var set;


            function appendCountry(){                   

                if($.inArray($("#businessCountryTarget option:selected").text(),countrySet) === -1) {                           

                $("#businessDisplayCountryInfo").append($("#businessCountryTarget option:selected").text() + "<br>");           

                       countrySet.push($("#businessCountryTarget option:selected").text());
                    set = { country: $("#businessCountryTarget option:selected").text()};

        }

            }



            function appendCity(){                  

                if($.inArray($("#businessCityTarget option:selected").text(),citySet) === -1){                          

                        $("#cityContainer").append($("#businessCityTarget option:selected").text() + "<br>"); 

                       $("#radiusContainer").append($("#businessCityRadius option:selected").text() + "<br>"); 
                 citySet.push($("#businessCityTarget option:selected").text(),+'-'+$("#businessCityRadius option:selected").text());    

                set = {city: $("#businessCityTarget option:selected").text()};
                radius = $("#businessCityRadius option:selected").text();
                set = {radius: $("#businessCityRadius option:selected").text()};

                }       

            }


    function SaveLocations(){
            alert(set['country'] + ' - '+ set['city'] +'-'+ set['radius'] +"<br>");

        }
var countrySet=新数组;
var citySet=新数组;
var radiusSet=新数组;
var集;
函数appendCountry(){
if($.inArray($(“#businessCountryTarget选项:已选定”).text(),countrySet)=-1{
$(“#businessDisplayCountryInfo”).append($(“#businessCountryTarget选项:选中”).text()+“
”); countrySet.push($(“#businessCountryTarget选项:选中”).text(); set={country:$(“#businessCountryTarget选项:选中”).text(); } } 函数appendCity(){ if($.inArray($(“#businessCityTarget选项:选中”).text(),citySet)=-1{ $(“#cityContainer”).append($(“#businessCityTarget选项:选中”).text()+“
”); $(“#radiusContainer”).append($(“#businessCityRadius选项:选中”).text()+“
”; citySet.push($(“#businessCityTarget选项:选中”).text(),+'-'+$(“#businessCityRadius选项:选中”).text(); set={city:$(“#businessCityTarget选项:选中”).text()}; 半径=$(“#businessCityRadius选项:选中”).text(); set={radius:$(“#businessCityRadius选项:选中”).text()}; } } 函数SaveLocations(){ 警报(设置['country']+'-'+设置['city']+'-'+设置['radius']+“
”); }
警报框中仅显示最后一项。其他的则显示为未定义

请帮忙


Mike

JS中有关联数组。您只需执行以下操作:

var myarray = { "key": "Value" }

Javascript的本机对象是键值对,允许您将它们视为映射,大致相当于关联数组。因此,上面的代码实际上是完全有效的javascript

var set;
set = { country: "England", city: "London", radius: "10 miles" };
然后可以使用访问值

set["country"] //"England"


只要你声明:

var x = []; // you actually don't need new Array()
可以将x视为关联数组

x.a = 10;
这相当于

var x = {a: 10};
所以


您可以使用像map这样的对象(没有顺序的关联数组),如果要将对象用作对象,则不要将其声明为数组。这误导到了极点。嗨,谢谢你的回复。我更新了问题和脚本,添加了一个新函数来提醒“set”中的所有值。但是它只显示国家。不过,您不再有明确的问题。我不知道你在问什么。请用一个明确的问题来重新表述这个问题,这个问题是你需要帮助的,比“代码不起作用”更具体
var x = {a: 10};
x.a == 10; // true

x['a'] == 10; // true