如果使用Array.filter(?)未定义值,则删除父对象和子对象

时间:2016-11-30 03:18:40

标签: javascript arrays

问题:

如何删除所有空的emailAddress,如果没有emailAddresses批准,也请删除该批准。

当emailAddress完全清空时,我当前的解决方案将删除批准。但是当两个emailAddresses存在且一个为空时(请参阅脚本输出与预期输出)

var request = {
 approvals: [
     {
       type: 'media',
       emailAddresses: [
        {emailAddress: 'frank@gmail.com'},
       ]
     },
     {
       type: 'other',
       emailAddresses: [
        {emailAddress: ''},
       ]
     },
     {
       type: 'scope',
       emailAddresses: [
        {emailAddress: 'kelly@yahoo.com'},
        {emailAddress: ''},
       ]
     }
   ] 
}
const filterOutEmptyEmails = (approval) => {
  if(approval.emailAddresses.filter(x => !!x.emailAddress).length){
    return true;  
  }
  
}

let output = request.approvals.filter(filterOutEmptyEmails);

console.log(JSON.stringify(output));
 
 

// EXPECTED OUTPUT:
// approval: [
//     {
//       type: 'media',
//       emailAddresses: [
//         {emailAddress: 'frank@gmail.com'},
//       ]
//     },
//     {
//       type: 'scope',
//       emailAddresses: [
//         {emailAddress: 'kelly@yahoo.com'},
//       ]
//     }
//   ] 
// }]

Live Code

3 个答案:

答案 0 :(得分:2)

您没有在代码中替换approval.emailAddresses - 您应该使用:

approval.emailAddresses = approval.emailAddresses.filter(x => !!x.emailAddress);

见下面的演示:



var request={approvals:[{type:'media',emailAddresses:[{emailAddress:'frank@gmail.com'},]},{type:'other',emailAddresses:[{emailAddress:''},]},{type:'scope',emailAddresses:[{emailAddress:'kelly@yahoo.com'},{emailAddress:''},]}]};
                        
var filterOutEmptyEmails = (approval) => {
   approval.emailAddresses = approval.emailAddresses.filter(x => !!x.emailAddress);
  if(approval.emailAddresses.length){
    return true;  
  }
}

var output = request.approvals.filter(filterOutEmptyEmails);

console.log(JSON.stringify(output));




编辑

另一个没有改变输入数组的提议 - 使用Array.prototype.reduce创建一个新数组:



var request={approvals:[{type:'media',emailAddresses:[{emailAddress:'frank@gmail.com'},]},{type:'other',emailAddresses:[{emailAddress:''},]},{type:'scope',emailAddresses:[{emailAddress:'kelly@yahoo.com'},{emailAddress:''},]}]};
                        

var output = request.approvals.reduce(function(p,c){
  // creates a shallow copy
  var elem = Object.assign({},c);
  // replaces the reference to request.approvals by the new array created by the filter
  elem.emailAddresses = elem.emailAddresses.filter(x => !!x.emailAddress);
  if(elem.emailAddresses.length != 0)
    p.push(elem);
  return p;
},[]);

// console.log(request.approvals);
console.log(output);

.as-console-wrapper{top:0;max-height:100%!important;}




答案 1 :(得分:1)

可能"非突变"解决方案可能就像这样



var request = {approvals: [{type: 'media',emailAddresses: [{emailAddress: 'frank@gmail.com'},]},{type: 'other',emailAddresses: [{emailAddress: ''},]},{type: 'scope', emailAddresses: [{emailAddress: 'kelly@yahoo.com'},{emailAddress: ''},]}]}

    const filterOutEmptyEmails = (approval) => {
      if(approval.emailAddresses.filter(x => !!x.emailAddress).length){
        return true;  
      }
      
    }
       
    const output = request.approvals.map(approval => {
      const filteredAproval = approval;
      filteredAproval.emailAddresses = approval.emailAddresses.filter(x => !!x.emailAddress);
      return filteredAproval
    }).filter(filterOutEmptyEmails);
    

    console.log(JSON.stringify(output));
    console.log(JSON.stringify(request));




答案 2 :(得分:1)

没有突变(含有大量的ES6 / 7糖):

const filteredApprovals = request.approvals.reduce((acc, approval) => {
  const filteredEmailAddresses = approval.emailAddresses.filter(item => item.emailAddress);
  return (filteredEmailAddresses.length > 0) ? [...acc, { ...approval, emailAddresses: filteredEmailAddresses }] : acc;
}, []);

小提琴:https://jsfiddle.net/free_soul/hndjbce3/