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

时间:2023-01-12 13:20:16

I have a big JSON data, this is a small part:

我有一个很大的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"
    }
}]

I need to change value 'activated' to 'deactivated', 'archived' to 'active' in whole array. I think I need to use for loop, but I don't know how to properly write it.

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

4 个解决方案

#1


1  

Use the Array prototype map function :

使用Array原型图功能:

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

Update. A faster approach.

更新。更快的方法。

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

You cannot do it faster or more narrow.

你不能更快或更窄。

#2


1  

Try the following with array's map().

使用数组的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);

#3


1  

this is a possible way (Array#extras):

这是一种可能的方式(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);

#4


0  

Do not know if your intentions about to mutate original object or just return a new array of objects. But below code might give you an inspiration.

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

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

then,

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

#1


1  

Use the Array prototype map function :

使用Array原型图功能:

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

Update. A faster approach.

更新。更快的方法。

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

You cannot do it faster or more narrow.

你不能更快或更窄。

#2


1  

Try the following with array's map().

使用数组的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);

#3


1  

this is a possible way (Array#extras):

这是一种可能的方式(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);

#4


0  

Do not know if your intentions about to mutate original object or just return a new array of objects. But below code might give you an inspiration.

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

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

then,

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