如何通过JS更改符合条件的数组中的值

时间:2017-12-28 15:35:03

标签: javascript arrays json

我有一个很大的JSON数据,这只是一小部分:

users = [{
  "name": "alex", 
   "id":123, 
   "surname":"xx",
   "status":"activated",  
    tarriff: {
      "id":1,
      "name":"free"
    }
},
 {
  "name": "tom", 
   "id":124, 
   "surname":"henry",
   "status":"activated", 
    tarriff: {
      "id":1,
      "name":"free"
    }
},
 {
  "name": "tom", 
   "id":125, 
   "surname":"henry",
   "status":"archived",  
    tarriff: {
      "id":1,
      "name":"free"
    }
}]

我需要在整个数组中将值'activated'更改为'deactivated',将'archived'更改为'active'。 我想我需要使用for循环,但我不知道如何正确编写它。

4 个答案:

答案 0 :(得分:1)

使用数组map()尝试以下操作。

var users = [{
  "name": "alex", 
   "id":123, 
   "surname":"xx",
   "status":"activated",  
    tarriff: {
      "id":1,
      "name":"free"
    }
},
 {
  "name": "tom", 
   "id":124, 
   "surname":"henry",
   "status":"activated", 
    tarriff: {
      "id":1,
      "name":"free"
    }
},
 {
  "name": "tom", 
   "id":125, 
   "surname":"henry",
   "status":"archived",  
    tarriff: {
      "id":1,
      "name":"free"
    }
}];

var res = users.map(function(item){
  if(item.status == 'activated')
    item.status = 'deactivated';
  if(item.status == 'archived')
    item.status = 'active';
  return item;
});

console.log(res);

答案 1 :(得分:1)

这是一种可能的方式(Array#extras):

function changeUserStatus(status) {
  // 'activated' to 'deactivated', 'archived' to 'active'
  
  switch (status) {
    case 'activated':
      return 'deactivated';
      
    case 'archived':
      return 'active';
    
    default:
      return status;
  }
}

function changeStatus(users) {
  return users.map(function(user) {
    
    return Object.assign({}, user, {
      status: changeUserStatus(user.status),
    });
  });
}

var users = [{
  "name": "alex", 
   "id":123, 
   "surname":"xx",
   "status":"activated",  
    tarriff: {
      "id":1,
      "name":"free"
    }
},
 {
  "name": "tom", 
   "id":124, 
   "surname":"henry",
   "status":"activated", 
    tarriff: {
      "id":1,
      "name":"free"
    }
},
 {
  "name": "tom", 
   "id":125, 
   "surname":"henry",
   "status":"archived",  
    tarriff: {
      "id":1,
      "name":"free"
    }
}];

const edited = changeStatus(users);

console.log('edited', edited);

答案 2 :(得分:1)

使用Array原型map功能:

users = users.map(function(u) {
  if (u.status == 'activated') u.status = 'deactivated'
  if (u.status == 'archived') u.status = 'active'
  return u
})

<强>更新即可。更快的方法。

var counter, item;
for (counter in users) {
  item = users[counter];
  if (item.status == 'activated') item.status = 'deactivated'
  if (item.status == 'archived') item.status = 'active'
}

你不能更快或更窄。

答案 3 :(得分:0)

不知道你的意图是要改变原始对象还是只返回一个新的对象数组。但是下面的代码可能会给你一个灵感。

function changeValue(obj, k, vold, vnew) {
  if (obj.hasOwnProperty(k) && obj[k] == vold) {
      obj[k] = vnew 
  }
  return obj;
}

然后,

users
  .map(e => changeValue(e, 'status', 'activated', 'deactivated'))
  .map(e => changeValue(e, 'status', 'archived', 'active'));