如何访问数组中的另一个数组

时间:2017-11-07 06:41:50

标签: javascript

大家好我想知道如何显示"产品的数据"在数组中,请参阅下面的代码

JSON数组

var places = [
{"country_name": "Denmark", "latitude": 56, "longitude": 10, "status": "OK", "site_name": "Denmark", "serial_number": "12345", "products":["1","2"]},
{"country_name": "Japan", "latitude": 56, "longitude": 10, "status": "OK", "site_name": "Norway", "serial_number": "12345", "products":["3","4"]},
{"country_name": "Denmark", "latitude": 56, "longitude": 10, "status": "OK", "site_name": "USA", "serial_number": "12345", "products":["7","6"]}]

的Javascript

function displayData(e)
{
var html = '';
var mapDiv = document.getElementById('mapContainer'), i = 0,
    dataIndex, tooltipDiv, key,
    mapMarkers = $(mapDiv).find('.e-mapMarker'), index = 0;
var collection = [];

for (i = 0; i < mapMarkers.length; i++) {

    if (e.target.parentNode.parentNode == mapMarkers[i]) {
        index = i;

    }
}

html += '<div id="infocontainer" class="map_element">';
html += '<div class="p-image"></div>';
html += '<div class="popupdetail">';
html += '<div class="p-name"> Site Name: ' + places[index].site_name + '</div>';
html += '<div class="p-name"> Site Status: ' + places[index].status + '</div>';
html += '<div class="p-name"> Country: ' + places[index].country_name + '</div>';
html += '</div>';
html += '</div>';


if (!document.getElementById('map_tooltip'))
{
    tooltipdiv = $("<div></div>").attr('id', "map_tooltip");
    $(document.body).append(tooltipdiv);
    $(tooltipdiv).css({
        "display": "none", "padding": "5px",
        "position": "absolute",
        "z-index": "13000",
        "cursor": "default",
        "font-family": "Segoe UI",
        "color": "#707070",
        "font-size": "12px", "pointer-events": "none",
        "background-color": "#FFFFFF",
        "border": "1px solid #707070"
    });
}
else
   {
    tooltipdiv = $("#map_tooltip");
    $(tooltipdiv).css({
        "left": (e.pageX + 5),
        "top": (e.pageY + 5)
    });
    $(tooltipdiv).html(html).show("slow");
   }
 }

所以我想渲染&#34; 1&#34;,&#34; 2&#34;在产品内部,或者我在阵列中有多个国家/地区,我想呈现一个国家的特定产品数组,所以我希望我不会因为不清楚的英语或解释而得到任何投票: - )

4 个答案:

答案 0 :(得分:1)

你可以循环遍历数组:

&#13;
&#13;
var places = [
{"country_name": "Denmark", "latitude": 56, "longitude": 10, "status": "OK", "site_name": "Denmark", "serial_number": "12345", "products":["1","2"]},
{"country_name": "France", "latitude": 5644, "longitude": 1044, "status": "OK", "site_name": "France", "serial_number": "125", "products":["3","4"]}]

places.forEach(function(item, index){
  if(item.country_name == "Denmark"){
    if(item.products){
      item.products.forEach(function(val){
        console.log(val);
      });
    }
  }
});
&#13;
&#13;
&#13;

答案 1 :(得分:0)

  • places[0].products[0]获取价值&#34; 1&#34;
  • places[0].products[1]获取价值&#34; 2&#34;

places[0].products.join(',')加入以逗号分隔的两个值(,

答案 2 :(得分:0)

在我看来,更简单的循环。

var places = [
    {"country_name": "Denmark", "latitude": 56, "longitude": 10, "status": 
     "OK", "site_name": "Denmark", "serial_number": "12345", "products":
     ["1","2"]},
    {"country_name": "Denmark", "latitude": 56, "longitude": 10, "status": 
     "OK", "site_name": "Denmark", "serial_number": "12345", "products":
     ["1","2"]}
    ]
for(var j = 0;j<places.length;j++){
   if(places[j].products){
    var plcs = places[j].products
    for(var i = 0;i < plcs.length;i++){
    console.log(plcs[i]);
     }
   }
 }

答案 3 :(得分:-1)

试试这个

places[index].products.join(',')

希望这就是你所需要的。