如何使用jquery显示子数组中的特定数据?

时间:2017-07-04 06:36:32

标签: javascript jquery

你好我们很困惑,并没有更多的想法,如何附加特定的json字符串从子数组中选择标签。我已经在互联网上搜索了但是一些答案和问题与我的问题不符。

array (size=6)
  0 => 
    array (size=16)
      'id' => int 7
      'receive_purchase_series_id' => null
      'lumber_processing_series_id' => int 174
      'items_series_code' => string 'LP-001-id_1-001-001' (length=19)
      'quantity' => int 50
      'type_id' => int 7
      'supplier_id' => null
      'status' => int 0
      'created_by_id' => int 49
      'scanned_by_id' => int 49
      'receive_purchase_code_from' => null
      'lumber_processing_code_from' => string '191589764523' (length=12)
      'item_id' => int 40
      'created_at' => string '2017-06-30 08:40:17' (length=19)
      'updated_at' => string '2017-06-30 08:40:17' (length=19)
      'nail_items' => 
        array (size=6)
          'id' => int 40
          'type_id' => int 7
          'code' => string 'LI-7' (length=4)
          'description' => string '90X120X200' (length=10)
          'created_at' => string '2017-05-15 11:29:12' (length=19)
          'updated_at' => string '2017-05-15 11:29:12' (length=19)
  1 => 
    array (size=16)
      'id' => int 8
      'receive_purchase_series_id' => null
      'lumber_processing_series_id' => int 175
      'items_series_code' => string 'LP-001-id_1-001-002' (length=19)
      'quantity' => int 50
      'type_id' => int 7
      'supplier_id' => null
      'status' => int 0
      'created_by_id' => int 49
      'scanned_by_id' => int 49
      'receive_purchase_code_from' => null
      'lumber_processing_code_from' => string '191589764523' (length=12)
      'item_id' => int 40
      'created_at' => string '2017-07-03 08:57:47' (length=19)
      'updated_at' => string '2017-07-03 08:57:47' (length=19)
      'nail_items' => 
        array (size=6)
          'id' => int 40
          'type_id' => int 7
          'code' => string 'LI-7' (length=4)
          'description' => string '90X120X200' (length=10)
          'created_at' => string '2017-05-15 11:29:12' (length=19)
          'updated_at' => string '2017-05-15 11:29:12' (length=19)
  2 => 
    array (size=16)
      'id' => int 9
      'receive_purchase_series_id' => null
      'lumber_processing_series_id' => int 176
      'items_series_code' => string 'LP-001-id_1-002-001' (length=19)
      'quantity' => int 60
      'type_id' => int 8
      'supplier_id' => null
      'status' => int 0
      'created_by_id' => int 49
      'scanned_by_id' => int 49
      'receive_purchase_code_from' => null
      'lumber_processing_code_from' => string '430159790249' (length=12)
      'item_id' => int 38
      'created_at' => string '2017-07-03 09:01:23' (length=19)
      'updated_at' => string '2017-07-03 09:01:23' (length=19)
      'nail_items' => 
        array (size=6)
          'id' => int 38
          'type_id' => int 8
          'code' => string 'LI-5' (length=4)
          'description' => string '18X127X1000' (length=11)
          'created_at' => string '2017-05-09 16:41:39' (length=19)
          'updated_at' => string '2017-05-15 10:37:13' (length=19)
  3 => 
    array (size=4)
      'nail_id' => int 27
      'code' => string 'NI-1' (length=4)
      'description' => string 'NAIL ITEM 1' (length=11)
      'type' => null
  4 => 
    array (size=4)
      'nail_id' => int 28
      'code' => string 'NI-2' (length=4)
      'description' => string 'NAIL ITEM 2' (length=11)
      'type' => null
  5 => 
    array (size=4)
      'nail_id' => int 29
      'code' => string 'NI-3' (length=4)
      'description' => string 'NAIL ITEM 3' (length=11)
      'type' => null

这是我现有的代码......

$.each(active_items, function(key,value){
  rowTemp1 += "<option value='"+ value['nail_id'] +"'>"+ value['code'] +" "+         value['description'] +"</option>";
      $.each(value['nail_items']  , function(key2,item){
         rowTemp1 += "<option >"+ item['code'] +"</option>";
     });
});

1 个答案:

答案 0 :(得分:0)

检查第一个数组中是否存在密钥,然后在子数组中搜索。请尝试以下代码:

var rowTemp1 ="";
$.each(active_items, function(key,value){
      if('nail_id' in value){
         rowTemp1 += "<option value='"+ value['nail_id'] +"'>"+value['code'] +" "+value['description'] +"</option>";
      }else{


           rowTemp1 += "<option >"+ value['nail_items']['code']  +"</option>";

     }
});

<强> Working demo