【问题标题】:javascript update the property value if not true inside array of objectsjavascript 如果在对象数组中不为真,则更新属性值
【发布时间】:2018-05-14 11:21:19
【问题描述】:

给定以下对象:

const kids = {
    name: 'john',
    extra: {
        city: 'London',
        hobbies: [
            {
                id: 'football',
                team: 'ABC',
                joined: true
            },
            {
                id: 'basketball',
                team: 'DEF',
                joined: false
            },
            {
                id: 'volleyball',
                team: 'DEF',
                joined: null
            },
        ],
    },
};

我想更新爱好数组的每个对象中的joined 字段,如果不是true,则将它们设置为null

问题是我如何使用地图和查找来做到这一点?并更新当前变量而不创建新变量。

【问题讨论】:

  • 好的..祝你好运..
  • 您有问题吗?

标签: javascript arrays ecmascript-6


【解决方案1】:

您可以为此使用Array#forEachfind 太过分了,map 真的不需要,因为你没有从迭代中返回任何东西。

const kids = {
  name: 'john',
  extra: {
    city: 'London',
    hobbies: [{
        id: 'football',
        team: 'ABC',
        joined: true
      },
      {
        id: 'basketball',
        team: 'DEF',
        joined: false
      },
      {
        id: 'volleyball',
        team: 'DEF',
        joined: null
      },
    ],
  },
};

kids.extra.hobbies.forEach(item => {
  if (item.joined !== true) {
    item.joined = null
  }
})

console.log(kids);

【讨论】:

    【解决方案2】:

    你可以使用Array.prototype.forEach()

    代码:

    const kids = {name: 'john',extra: {city: 'London',hobbies: [{id: 'football',team: 'ABC',joined: true},{id: 'basketball',team: 'DEF',joined: false},{id: 'volleyball',team: 'DEF',joined: null},]}};
    kids.extra.hobbies.forEach(hobby => hobby.joined = hobby.joined || null);
    
    console.log(kids.extra.hobbies);

    【讨论】:

      【解决方案3】:

      遍历 hobbies 数组并将连接字段的默认值设置为 null:

      const kids = {
          name: 'john',
          extra: {
              city: 'London',
              hobbies: [
                  {
                      id: 'football',
                      team: 'ABC',
                      joined: true
                  },
                  {
                      id: 'basketball',
                      team: 'DEF',
                      joined: false
                  },
                  {
                      id: 'volleyball',
                      team: 'DEF',
                      joined: null
                  },
              ],
          },
      };
      
      kids.extra.hobbies.forEach(h => h.joined = h.joined || null);
      
      console.log(kids);

      【讨论】:

        【解决方案4】:

        下面的代码 sn-p 向您展示了如何做到这一点。只需循环遍历数组并放入 if 语句。

        const kids = {
            name: 'john',
            extra: {
                city: 'London',
                hobbies: [
                    {
                        id: 'football',
                        team: 'ABC',
                        joined: true
                    },
                    {
                        id: 'basketball',
                        team: 'DEF',
                        joined: false
                    },
                    {
                        id: 'volleyball',
                        team: 'DEF',
                        joined: null
                    },
                ],
            },
        };
        for(var i=0;i<kids.extra.hobbies.length;i++){
          var data = kids.extra.hobbies[i];
          if(data.joined !== true){
            data.joined = null;
          }
        }
        console.log(kids)

        【讨论】:

        • 不应该是data,joined !== true吗?否则“真实”值也会通过。如果没关系,也许只需说 if (data.joined) 并完全跳过比较...
        【解决方案5】:

        使用数组 map 判断是否为 false ,然后设置为 null,map 会返回一个新数组。然后将其设置为 kids.extra.hobbies

        const kids = {
          name: 'john',
          extra: {
            city: 'London',
            hobbies: [{
                id: 'football',
                team: 'ABC',
                joined: true
              },
              {
                id: 'basketball',
                team: 'DEF',
                joined: false
              },
              {
                id: 'volleyball',
                team: 'DEF',
                joined: null
              },
            ],
          },
        };
        var x = kids.extra.hobbies.map(function(item) {
          if (item.joined === false) {
            item.joined = null;
            return item
          } else {
            return item;
          }
        
        })
        kids.extra.hobbies = x;
        console.log(kids)

        【讨论】:

        • 我相信OP想要修改键值对就地,这不是替换hobbies数组吗?我承认这个问题不是很清楚。
        【解决方案6】:

        你可以试试这个吗?

        $(document).ready(function() {
                $(kids.extra.hobbies).each(function(){
                    if(this.joined !== true){
                        this.joined = null;
                        alert(this.joined)
                    }
                });
            });
        

        【讨论】:

        • 哦,我的错。很抱歉给您带来不便。
        猜你喜欢
        • 2018-04-30
        • 2021-01-12
        • 1970-01-01
        • 2019-09-15
        • 1970-01-01
        • 2019-05-02
        • 1970-01-01
        • 2017-08-08
        • 2021-12-06
        相关资源
        最近更新 更多