【问题标题】:TypeError: Cannot read property 'MyProperty' of undefinedTypeError:无法读取未定义的属性“MyProperty”
【发布时间】:2021-08-05 22:39:50
【问题描述】:

我有一个这样的对象对数组:

0: pairs {_stim1: {img: "images/F111C.png", gender: 0, rating: "3"}, _stim2: {img: "images/F042C.png", gender: 0, rating: "3"}}
1: pairs {_stim1: {…}, _stim2: {…}}
2: pairs {_stim1: {…}, _stim2: {…}}
3: pairs {_stim1: {…}, _stim2: {…}}
4: pairs {_stim1: {…}, _stim2: {…}}
5: pairs {_stim1: {…}, _stim2: {…}}
6: pairs {_stim1: {…}, _stim2: {…}}
7: pairs {_stim1: {…}, _stim2: {…}}

我正在尝试将数组减少到 6 对,并且每个性别属性(1 或 0)尽可能接近 3 对。为此,我有这个笨拙的解决方案,我遍历列表并将性别为 0 的 3 对对象附加到列表中,然后将性别为 1 的 3 对对象附加到列表中,如下所示:

pair_count = 0;
if ((stim_pairs.length > 6)) {
    for (var i, _pj_c = 0, _pj_a = stim_pairs, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
        i = _pj_a[_pj_c];
        if ((pair_count < 3)) {
            if ((i.stim1.gender === 0)) {
                initial_stim_pairs.push(i);
                final_stim_pairs.push(i);
                pair_count += 1;
            }
        }
    }
    for (var i, _pj_c = 0, _pj_a = stim_pairs, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
        i = _pj_a[_pj_c];
        if ((pair_count < 6)) {
            if ((i.stim1.gender === 1)) {
                initial_stim_pairs.push(i);
                final_stim_pairs.push(i);
                pair_count += 1;
            }
        }
    }
}

但是,我的 'i.stim1' 和 'i.stim2' 返回 'undefined'... 我得到了这个错误:

'无法读取未定义的属性'性别''

我做错了什么?

【问题讨论】:

    标签: javascript object properties undefined typeerror


    【解决方案1】:

    您可以验证 i.stim1.gender 属性是否存在

    if(typeof i.stim1.gender !== 'undefined'){ //your code here}
    

    【讨论】:

      【解决方案2】:

      我已从您的代码中解决了您的错误,因此它可以正常工作,但您不会从中得到正确的答案。最初的问题是它应该是i._stim1.gender 而不是i.stim1.gender。之后缺少一些声明,请确保使用调试器并在控制台上运行此代码以了解问题。

      var pair_count = 0,
      initial_stim_pairs = [],
      final_stim_pairs = [];
      if ((stim_pairs.length > 6)) {
          for (var i, _pj_c = 0, _pj_a = stim_pairs, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c ++) {
              i = _pj_a[_pj_c];
      debugger;
              if ((pair_count < 3)) {
                  if ((i._stim1.gender === 0)) {
                      initial_stim_pairs.push(i);
                      final_stim_pairs.push(i);
                      pair_count += 1;
                  }
              }
          }
          for (var i, _pj_c = 0, _pj_a = stim_pairs, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
              i = _pj_a[_pj_c];
              if ((pair_count < 6)) {
                  if ((i._stim1.gender === 1)) {
                      initial_stim_pairs.push(i);
                      final_stim_pairs.push(i);
                      pair_count += 1;
                  }
              }
          }
      }
      

      【讨论】:

      • 感谢分配!这让它工作了。但是你的意思是我不会得到正确的答案?你觉得逻辑有问题吗?
      • 我没有深入研究逻辑,只是解决了您的错误。所以不确定逻辑是否正确。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 2019-08-19
      相关资源
      最近更新 更多