【问题标题】:How to get object property list data如何获取对象属性列表数据
【发布时间】:2021-11-16 07:52:05
【问题描述】:

我有一个对象列表。每个对象都有一个属性,它是一个元素列表:

  { name       : 'Club 01'
  , id         : 1
  , form       : 45
  , points     : 0
  , tactics    : 'neutral'
  , played     : 0
  , gameset    : 0
  , playedWith : [ 8, 1, 2, 3 ] 
  } 

我想通过列表和控制台记录所有现有元素:

for (let a = 0; a<clubs.width; a++) {
  for (let b = 0; b<clubs[a].playedWith.width; b++) {
    console.log(clubs[a].playedWith[b]);
  }
}

当我为一件物品做这件事时,这很有效。但是,当我使用循环作为 aboce 时,这让我想到了

undefined

我的代码有什么问题?如何控制台记录 playWith 属性中的所有项目?

【问题讨论】:

  • 为什么是clubs[a].playedWith.WIDTH?不是.length?

标签: javascript arrays loops properties


【解决方案1】:

两个循环都必须使用length 而不是width。

这是一个例子:

 var clubs = [
 { name       : 'Club 01'
  , id         : 1
  , form       : 45
  , points     : 0
  , tactics    : 'neutral'
  , played     : 0
  , gameset    : 0
  , playedWith : [ 8, 1, 2, 3 ] 
  } 
];

for (let a = 0; a < clubs.length; a++) {
  for (let b = 0; b < clubs[a].playedWith.length; b++) {
    console.log(clubs[a].playedWith[b]);
  }
}

【讨论】:

    【解决方案2】:

    Elikill58 是对的。数组有 length 属性,而不是 width 属性。

    所以你的代码可以这样运行:

    for (let a = 0; a < clubs.length; a++){
      for (let b = 0; b < clubs[a].playedWith.length; b++){
        console.log(clubs[a].playedWith[b]);
      }
    }
    

    另外,如果你想遍历数组中的所有项,为了简单起见,你可以这样写:

    for (const club of clubs) {
      for (const width of club.playedWith) {
        console.log(width);
      }
    }
    

    【讨论】:

      【解决方案3】:

      您无法使用宽度获取数组长度。 试试这个,

      var clubs = [{name: 'Club 01', id:1, form: 45, points: 0, tactics: 'neutral', played: 0, gameset: 0, playedWith: [8,1,2,3]}]
      
      
      for (let club of clubs) {
        for(let playedWothId of club.playedWith){
          console.log(playedWothId);
        }
          
      }
      

      【讨论】:

        【解决方案4】:

        最好的办法就是这样做:

        Object.keys(data).forEach((key, i) => {
          console.log("Property: " + key, "\nValue: ", data[key])
        })
        
        

        这将为您提供属性和价值。您还可以对其进行调整,以获得更强大的逻辑来查找或解析不同的数据类型。

        您的一个属性中有一个数组。你可以这样处理。

        let clubs = [ {}, {} ]
        
          clubs.forEach((club, i) => {
             
              if (club && Array.isArray(club) || typeof club !== 'object') {
                 return;
                 // Stop loop bc club is not an object
              }    
        
               Object.keys(club).forEach((key, i) => { // Iterate through all the object properties
               console.log("Property: " + key, "\nValue: ", club[key])
          
              if (Array.isArray(club[key]) { // Add more conditions or look for certain property names (keys).
                console.log('Length of array from property: ', club[key].length)
                club[key].map(el => console.log(key + ' value: ' + el))
                // You can choose how you want to handle this. 
         
                /* 
                   Expected output: 
                   playedWith value: 8
                   playedWith value: 1
                   playedWith value: 2
                   playedWith value: 3
          
                */
              }
        
            })
        
        
          })
        
        

        【讨论】:

          【解决方案5】:

          让 b = { 名称:“俱乐部 01”, 编号:1, 形式:45, 分:0, 战术:“中立”, 播放:0, 游戏集:0, 播放:[8,1,2,3],

          move: function() {
              return `
              ${ this.name }and
              ${this.id }and
              ${ this.form }and
              ${ this.points }and
              ${ this.tactics }and
              ${ this.played }and
              ${ this.gameset }and
              ${ this.playedWith }`
          }
          

          };

          console.log(b.move()) for (var w in b) { 控制台.log(${w}:${b.move()}) }

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-09-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-29
            • 2021-02-17
            • 1970-01-01
            相关资源
            最近更新 更多