无法从数组中获取相关值,出现 Uncaught TypeError "Cannot use 'in' operator to search 'length"

Posted

技术标签:

【中文标题】无法从数组中获取相关值,出现 Uncaught TypeError "Cannot use \'in\' operator to search \'length"【英文标题】:Not able to get the relevant value from array, getting Uncaught TypeError "Cannot use 'in' operator to search for 'length"无法从数组中获取相关值,出现 Uncaught TypeError "Cannot use 'in' operator to search 'length" 【发布时间】:2021-10-06 03:48:45 【问题描述】:

我需要从数组中获取所选选项的相关值。

我的选择:

<select id="BestMobileSelect">
    <option></option>
    <option value=Mobile-SmartPhone-Motorola>Motorola</option>
    <option value=Mobile-SmartPhone-Samsung>Samsung</option>
    <option value=Mobile-SmartPhone-Xiaomi>Xiaomi</option>
</select>

数组:

var BestMobileJSON = 
    "Mobile-SmartPhone-Motorola" : ["Price":"Rs.12000"],
    "Mobile-SmartPhone-Samsung" : ["Price":"Rs.15000"],
    "Mobile-SmartPhone-Xiaomi" : ["Price":"Rs.9000"],

我用来获取相关值的jquery如下:

$("#BestMobileSelect").on("change", function () 
        var BestMobileSelected = $(this).val()

        var markup = '';
        jQuery.each(BestMobileSelected, function(index, value)
            markup += '<div>' + BestMobileJSON[value][0].Price +'</div>'
        );

    $("#BestMobileResult").html(markup);
    );

但是,这不起作用,我收到错误 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in Mobile-SmartPhone-Motorola

为什么这不起作用?

在下面找到完整的代码:

jQuery(document).ready(function($) 
  var BestMobileJSON = 
    "Mobile-SmartPhone-Motorola": [
      "Price": "Rs.12000"
    ],
    "Mobile-SmartPhone-Samsung": [
      "Price": "Rs.15000"
    ],
    "Mobile-SmartPhone-Xiaomi": [
      "Price": "Rs.9000"
    ],
  

  $("#BestMobileSelect").on("change", function() 
    var BestMobileSelected = $(this).val()

    var markup = '';
    jQuery.each(BestMobileSelected, function(index, value) 
      markup += '<div>' + BestMobileJSON[value][0].Price + '</div>'
    );

    $("#BestMobileResult").html(markup);
  );

);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<select id="BestMobileSelect">
  <option></option>
  <option value=Mobile-SmartPhone-Motorola>Motorola</option>
  <option value=Mobile-SmartPhone-Samsung>Samsung</option>
  <option value=Mobile-SmartPhone-Xiaomi>Xiaomi</option>
</select>

<div id="BestMobileResult"></div>

【问题讨论】:

$.each() 的第一个参数必须是数组或对象。 BestMobileSelected 是一个字符串。 【参考方案1】:

$.each() 的参数必须是数组或对象,但 BestMobileSelected 是字符串。您应该使用下拉值作为BestMobileJSON 对象的索引;这将返回一个数组,然后您可以使用 $.each() 循环遍历该数组。

jQuery(document).ready(function($) 
  var BestMobileJSON = 
    "Mobile-SmartPhone-Motorola": [
      "Price": "Rs.12000"
    ],
    "Mobile-SmartPhone-Samsung": [
      "Price": "Rs.15000"
    ],
    "Mobile-SmartPhone-Xiaomi": [
      "Price": "Rs.9000"
    ],
  

  $("#BestMobileSelect").on("change", function() 
    var markup = '';
    let val = $(this).val();
    if (val) 
      var BestMobileSelected = BestMobileJSON[val];
      jQuery.each(BestMobileSelected, function(index, value) 
        markup += '<div>' + value.Price + '</div>'
      );
    
    $("#BestMobileResult").html(markup);
  );
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<select id="BestMobileSelect">
  <option></option>
  <option value=Mobile-SmartPhone-Motorola>Motorola</option>
  <option value=Mobile-SmartPhone-Samsung>Samsung</option>
  <option value=Mobile-SmartPhone-Xiaomi>Xiaomi</option>
</select>

<div id="BestMobileResult"></div>

【讨论】:

以上是关于无法从数组中获取相关值,出现 Uncaught TypeError "Cannot use 'in' operator to search 'length"的主要内容,如果未能解决你的问题,请参考以下文章

从数组中获取特定索引的值名称

从 Keil 中的 uint8_t 数组获取 uint16_t 值

Laravel - 无法使用刀片从多维数组中获取值

下划线 - 从数组中获取相同的值对象总和

无法从 BAT 文件中的数组中获取值

Codeigniter:从单列中获取所有值作为数组