2016-08-05 3 views
0

Unten ist mein JSON-Objekt, in dem ich die groupType aus der Gruppe entfernen möchte, trips.ie., trips.length===0 .And möchte auch das gesamte Client-Objekt, wenn alle die Fahrten Array ist für das jeweilige Client-Objekt leer ..Entfernen Sie Elemente in verschachtelten JSON basierend auf Bedingung

let fruitsArray= [ 
     { 
      "fruit": { 
       "id": 1, 
       "name": "Mango" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 1, 
         "name": "A" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 2, 
         "name": "B" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 3, 
         "name": "C" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 4, 
         "name": "D" 
        }, 
        "trips": [] 
       } 
      ] 
     }, 
     { 
      "fruit": { 
       "id": 1, 
       "name": "Apple" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 1, 
         "name": "A" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 2, 
         "name": "B" 
        }, 
        "trips": [ 
         { 
          "trip": { 
           "id": 62, 
           "deliverySlotId": 900000000000001 
          } 
         } 
        ] 
       }, 
       { 
        "groupType": { 
         "id": 3, 
         "name": "C" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 4, 
         "name": "D" 
        }, 
        "trips": [] 
       } 
      ] 
     }, 
     { 
      "fruit": { 
       "id": 95, 
       "name": "Banana" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 4, 
         "name": "A1" 
        }, 
        "trips": [ 
         { 
          "trip": { 
           "id": 62, 
           "deliverySlotId": 900000000000001 
          } 
         }, 
         { 
          "trip": { 
           "id": 63, 
           "deliverySlotId": 900000000000001 
          } 
         }, 
         { 
          "trip": { 
           "id": 65, 
           "deliverySlotId": 900000000000001, 
           "orderId": 22 
          } 
         } 
        ] 
       } 
      ] 
     } 
    ] 

der Code, den ich alle Erträge, die Elemente mit 0 groupType versucht haben ..

let finalArray = fruitsArray.map((group) => { 
    return group.group.filter((trip) => { 
     return trip.trips.length > 0; 
    })      
}) 
+0

Sie auch die ganze Frucht entfernen möchten, wenn es nicht drin Reisen sind? – JuHwon

+0

ja @ JuHwon.The ganze Frucht – forgottofly

+0

Statt Karte Sie reduzieren verwenden könnte und bedingt das Objekt schieben, wenn die Gruppe muß nicht Fahrten haben 'lassen finalArray = fruitsAray.reduce ((res, Gruppe) => { return res.concat (group.group.filter ((trip) => { zurück trip.trips.length> 0; }) || []); }, []) ' Ich habe den Code nicht getestet. Ich hoffe, du kommst auf die Idee. – z0mBi3

Antwort

1

Sie können map kombiniert mit filter. Alternativ können Sie auch die reduce Methode verwenden.

Ich schrieb beide Beispiele unter.

let fruitsArray= [ 
 
     { 
 
      "fruit": { 
 
       "id": 1, 
 
       "name": "Mango" 
 
      }, 
 
      "group": [ 
 
       { 
 
        "groupType": { 
 
         "id": 1, 
 
         "name": "A" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 2, 
 
         "name": "B" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 3, 
 
         "name": "C" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 4, 
 
         "name": "D" 
 
        }, 
 
        "trips": [] 
 
       } 
 
      ] 
 
     }, 
 
     { 
 
      "fruit": { 
 
       "id": 1, 
 
       "name": "Apple" 
 
      }, 
 
      "group": [ 
 
       { 
 
        "groupType": { 
 
         "id": 1, 
 
         "name": "A" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 2, 
 
         "name": "B" 
 
        }, 
 
        "trips": [ 
 
         { 
 
          "trip": { 
 
           "id": 62, 
 
           "deliverySlotId": 900000000000001 
 
          } 
 
         } 
 
        ] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 3, 
 
         "name": "C" 
 
        }, 
 
        "trips": [] 
 
       }, 
 
       { 
 
        "groupType": { 
 
         "id": 4, 
 
         "name": "D" 
 
        }, 
 
        "trips": [] 
 
       } 
 
      ] 
 
     }, 
 
     { 
 
      "fruit": { 
 
       "id": 95, 
 
       "name": "Banana" 
 
      }, 
 
      "group": [ 
 
       { 
 
        "groupType": { 
 
         "id": 4, 
 
         "name": "A1" 
 
        }, 
 
        "trips": [ 
 
         { 
 
          "trip": { 
 
           "id": 62, 
 
           "deliverySlotId": 900000000000001 
 
          } 
 
         }, 
 
         { 
 
          "trip": { 
 
           "id": 63, 
 
           "deliverySlotId": 900000000000001 
 
          } 
 
         }, 
 
         { 
 
          "trip": { 
 
           "id": 65, 
 
           "deliverySlotId": 900000000000001, 
 
           "orderId": 22 
 
          } 
 
         } 
 
        ] 
 
       } 
 
      ] 
 
     } 
 
    ]; 
 

 

 
var filteredFruits = fruitsArray.map((fruit) => { 
 
    fruit.group = fruit.group.filter((group) => group.trips.length); 
 
    return fruit; 
 
}).filter((fruit) => fruit.group.length); 
 

 
console.log(filteredFruits); 
 

 
// alternative example with reduce 
 
var reducedFruits = fruitsArray.reduce((prev, next) => { 
 
    next.group = next.group.filter((group) => group.trips.length); 
 
    return next.group.length ? prev.concat([next]) : prev; 
 
}, []); 
 

 
console.log(reducedFruits);

1
var fruitsArray= [ 
     { 
      "fruit": { 
       "id": 1, 
       "name": "Mango" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 1, 
         "name": "A" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 2, 
         "name": "B" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 3, 
         "name": "C" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 4, 
         "name": "D" 
        }, 
        "trips": [] 
       } 
      ] 
     }, 
     { 
      "fruit": { 
       "id": 1, 
       "name": "Apple" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 1, 
         "name": "A" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 2, 
         "name": "B" 
        }, 
        "trips": [ 
         { 
          "trip": { 
           "id": 62, 
           "deliverySlotId": 900000000000001 
          } 
         } 
        ] 
       }, 
       { 
        "groupType": { 
         "id": 3, 
         "name": "C" 
        }, 
        "trips": [] 
       }, 
       { 
        "groupType": { 
         "id": 4, 
         "name": "D" 
        }, 
        "trips": [] 
       } 
      ] 
     }, 
     { 
      "fruit": { 
       "id": 95, 
       "name": "Banana" 
      }, 
      "group": [ 
       { 
        "groupType": { 
         "id": 4, 
         "name": "A1" 
        }, 
        "trips": [ 
         { 
          "trip": { 
           "id": 62, 
           "deliverySlotId": 900000000000001 
          } 
         }, 
         { 
          "trip": { 
           "id": 63, 
           "deliverySlotId": 900000000000001 
          } 
         }, 
         { 
          "trip": { 
           "id": 65, 
           "deliverySlotId": 900000000000001, 
           "orderId": 22 
          } 
         } 
        ] 
       } 
      ] 
     } 
    ] 


    for(var i=0;i<fruitsArray.length;i++){ 
    var currentFruit = fruitsArray[i]; 
    var currentFruitGroups = currentFruit.group; 
     var foundTrip =false; 

     for(var j=0;j<currentFruitGroups.length;j++){ 

        var currentTrips = currentFruitGroups[j].trips; 

        if(!currentTrips.length){ 

       currentFruitGroups.splice(j,1); 
       j--; 
      }else{ 
       foundTrip = true; 
      } 
       } 
      if(!foundTrip){ 
      fruitsArray.splice(i,1); 
      i--; 
      } 
    } 
    console.log(fruitsArray); 

https://jsfiddle.net/485ue5zs/1/

1
let finalArray = fruitsArray.reduce((p, n) => { 
    const gt = n.group.filter(g => g.trips.length); 
    return gt.length ? p.concat({fruit: n.fruit, group: gt}) : p; 
}, []);