简体   繁体   English

如何使用jQuery从子数组显示特定数据?

[英]how to display specific data from sub-array using jquery?

hello guys im confused and don't have more idea, how to append specific json string to select tag from sub-array. 大家好,我很困惑,也没有更多的主意,如何附加特定的json字符串以从子数组中选择标签。 i have already searched over the internet but some answer and problem didn't match my issue. 我已经在互联网上进行搜索,但是一些答案和问题与我的问题不符。

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

This is my existing code... 这是我现有的代码...

$.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>";
     });
});

check if the key exist in first array if not then search in sub-array.Try below code : 检查键是否在第一个数组中,如果不存在则在子数组中搜索尝试以下代码:

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 工作演示

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

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