从JSON对象(React-Native js)中删除键值对?

时间:2017-07-17 05:06:32

标签: javascript android ios reactjs react-native

   [  
   {  
      id:265,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'parlo,',
      initialDate:'2017-07-06',
      finalDate:'2017-07-06',
      workingDays:1,
      employee:{  
         id:211,
         name:'appu'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         },
         {  
            id:4,
            name:'CANCEL'
         }
      ],
      halfDay:false
   },
   {  
      id:277,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'Venon,',
      initialDate:'2017-07-03',
      finalDate:'2017-07-04',
      workingDays:2,
      employee:{  
         id:206,
         name:'malya'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         },
         {  
            id:4,
            name:'CANCEL'
         }
      ],
      halfDay:false
   },
   {  
      id:285,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'ferrar,',
      initialDate:'2017-09-25',
      finalDate:'2017-09-25',
      workingDays:1,
      employee:{  
         id:68,
         name:'Geena'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         },
         {  
            id:4,
            name:'CANCEL'
         }
      ],
      halfDay:false
   },
   {  
      id:286,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'ole,',
      initialDate:'2017-09-26',
      finalDate:'2017-09-26',
      workingDays:1,
      employee:{  
         id:68,
         name:'harry'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         },
         {  
            id:4,
            name:'CANCEL'
         }
      ],
      halfDay:false
   }
]

以上是一个简单的json数组请建议我循环删除json数组的下面部分。下面的对象必须从数组中完全删除。我正在处理一个react-native项目所以代码必须反应js。

     {  
        id:4,
        name:'CANCEL'
     }

我的预期输出低于

[  
   {  
      id:265,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'parlo,',
      initialDate:'2017-07-06',
      finalDate:'2017-07-06',
      workingDays:1,
      employee:{  
         id:211,
         name:'appu'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         }
      ],
      halfDay:false
   },
   {  
      id:277,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'Venon,',
      initialDate:'2017-07-03',
      finalDate:'2017-07-04',
      workingDays:2,
      employee:{  
         id:206,
         name:'malya'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         }
      ],
      halfDay:false
   },
   {  
      id:285,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'ferrar,',
      initialDate:'2017-09-25',
      finalDate:'2017-09-25',
      workingDays:1,
      employee:{  
         id:68,
         name:'Geena'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         }
      ],
      halfDay:false
   },
   {  
      id:286,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'ole,',
      initialDate:'2017-09-26',
      finalDate:'2017-09-26',
      workingDays:1,
      employee:{  
         id:68,
         name:'harry'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         }
      ],
      halfDay:false
   }
]

3 个答案:

答案 0 :(得分:2)

这是一个使用for循环的简单解决方案。

 var json = [ { id:265, leaveType:{ id:1, name:'Casual Leave' }, currentProject:'parlo,', initialDate:'2017-07-06', finalDate:'2017-07-06', workingDays:1, employee:{ id:211, name:'appu' }, actionItems:[ { id:2, name:'APPROVE' }, { id:3, name:'REJECT' }, { id:4, name:'CANCEL' } ], halfDay:false }, { id:277, leaveType:{ id:1, name:'Casual Leave' }, currentProject:'Venon,', initialDate:'2017-07-03', finalDate:'2017-07-04', workingDays:2, employee:{ id:206, name:'malya' }, actionItems:[ { id:2, name:'APPROVE' }, { id:3, name:'REJECT' }, { id:4, name:'CANCEL' } ], halfDay:false }, { id:285, leaveType:{ id:1, name:'Casual Leave' }, currentProject:'ferrar,', initialDate:'2017-09-25', finalDate:'2017-09-25', workingDays:1, employee:{ id:68, name:'Geena' }, actionItems:[ { id:2, name:'APPROVE' }, { id:3, name:'REJECT' }, { id:4, name:'CANCEL' } ], halfDay:false }, { id:286, leaveType:{ id:1, name:'Casual Leave' }, currentProject:'ole,', initialDate:'2017-09-26', finalDate:'2017-09-26', workingDays:1, employee:{ id:68, name:'harry' }, actionItems:[ { id:2, name:'APPROVE' }, { id:3, name:'REJECT' }, { id:4, name:'CANCEL' } ], halfDay:false } ]
    ;
    
    for(var i =0;i<json.length;i++){
      actionItems = json[i].actionItems;
      for(var j =0;j<actionItems.length;j++){
         if(actionItems[j].name === 'CANCEL'){
           json[i].actionItems.splice(j,1);
          }
      }
    }
    
    console.log(json);

答案 1 :(得分:0)

您可以映射外部数组,然后根据actionItem name property

过滤项目

&#13;
&#13;
var data = [  
   {  
      id:265,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'parlo,',
      initialDate:'2017-07-06',
      finalDate:'2017-07-06',
      workingDays:1,
      employee:{  
         id:211,
         name:'appu'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         },
         {  
            id:4,
            name:'CANCEL'
         }
      ],
      halfDay:false
   },
   {  
      id:277,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'Venon,',
      initialDate:'2017-07-03',
      finalDate:'2017-07-04',
      workingDays:2,
      employee:{  
         id:206,
         name:'malya'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         },
         {  
            id:4,
            name:'CANCEL'
         }
      ],
      halfDay:false
   },
   {  
      id:285,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'ferrar,',
      initialDate:'2017-09-25',
      finalDate:'2017-09-25',
      workingDays:1,
      employee:{  
         id:68,
         name:'Geena'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         },
         {  
            id:4,
            name:'CANCEL'
         }
      ],
      halfDay:false
   },
   {  
      id:286,
      leaveType:{  
         id:1,
         name:'Casual Leave'
      },
      currentProject:'ole,',
      initialDate:'2017-09-26',
      finalDate:'2017-09-26',
      workingDays:1,
      employee:{  
         id:68,
         name:'harry'
      },
      actionItems:[  
         {  
            id:2,
            name:'APPROVE'
         },
         {  
            id:3,
            name:'REJECT'
         },
         {  
            id:4,
            name:'CANCEL'
         }
      ],
      halfDay:false
   }
]

var newData = data
   .map((ele)=>{
       let newElement = Object.assign({}, ele); 
    newElement.actionItems = newElement.actionItems.filter(actionItem => actionItem.name !== 'CANCEL');
    return newElement
   })
console.log(newData);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

首先,您需要确定数组中的匹配索引。 当你搜索一系列项目时,你看起来需要两个循环。

找到要删除的部分后,只需使用Array.splice(index, 1)完全删除数组中的密钥。

假设您要删除此部分

{  
    id:4,
    name:'CANCEL'
}

来自子对象的所有actionItems属性。 首先循环遍历您的对象集合,然后循环遍历您的操作集合

for (let i = 0; i < yourMainArray.length; i++) {
  let indexToDelete = -1
  for (let j = 0; j < yourMainArray[i].actionItems.length; j++) {
    if (yourMainArray[i].actionItems[j].name === 'CANCEL') {
      indexToDelete = j
    }
  }
  if (indexToDelete !== -1) {
    yourMainArray[i].actionItems.splice(indexToDelete, 1)
  }
}

由于您希望在同时循环时修改数组,您需要保留索引的引用以删除和删除循环外的项目。