如果在对象数组中不是true,则javascript更新属性值

时间:2018-05-14 11:21:20

标签: javascript arrays ecmascript-6

给出以下对象:

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字段,如果不是null,则将其设置为true;

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

6 个答案:

答案 0 :(得分:2)

您可以使用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);

答案 1 :(得分: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);




答案 2 :(得分:1)

遍历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);

答案 3 :(得分:1)

下面的代码段显示了如何执行此操作。只需循环遍历数组并输入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)

答案 4 :(得分:0)

使用数组映射并检查值是否为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)

答案 5 :(得分:0)

你能试试吗?

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