【JS】将对象数组中同一属性名的不同属性值的英文替换为中文

  1. 数据格式
arr:[
   {
    
    id:1,name:"名称1",status:"able"},
   {
    
    id:2,name:"名称2",status:"unable"},
   {
    
    id:3,name:"名称3",status:"able"},
]
  1. 需求

    (1)将status的属性值,翻译为各自的中文

 status 有俩种  able-"可能" unable-"不可能"
    (2) 对应后的结果
arr:[
  {
    
    id:1,name:"名称1",status:"可能"},
  {
    
    id:2,name:"名称2",status:"不可能"},
  {
    
    id:3,name:"名称3",status:"可能"},
]
  1. 转换

    (1)先循环数组
    (2)得到对象
    (3)在对象中进行替换

arr.forEach(item=>{
    
    
    if(item.status == 'able'){
    
    
       item.status = "可能"
    }else if(item.status == 'unable'){
    
    
       item.status = "不可能"
    }
})

猜你喜欢

转载自blog.csdn.net/weixin_47375144/article/details/129877657