2

I am trying to fetch all the costBreakdowns where the canceled status is false. So I am trying to filter() inside a filter() to fetch the complete array except the ones where the costBreakdowns have canceled is true:

const a = {
  "activeBookingsCostBreakdownGroups": [{
    "bookType": "RAIL",
    "costBreakdowns": [{
      "canceled": false,
      "totalInTripCurrency": {
        "amount": 43.39
      }
    }, ]
  }, {
    "bookType": "CAR",
    "costBreakdowns": [{
        "canceled": true,
        "totalInTripCurrency": {
          "amount": 86.78
        }
      },
      {
        "canceled": false,
        "totalInTripCurrency": {
          "amount": 87.79
        }
      },
    ]
  }]
}

console.log(
  a.activeBookingsCostBreakdownGroups.filter((elem) => (
    elem.costBreakdowns.filter(el => el.canceled === false)
  ))
)

But it somehow returns all the elements as though the filter() doesn't work.

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
Code_ninja
  • 21
  • 1

4 Answers4

1

I'd use reduce() were you first use filter on costBreakdowns to get an array of those where canceled is false.

Then if we;ve found anything (if (withoutFalse.length)) overwrite costBreakdowns with the result of filter (c.costBreakdowns = withoutFalse;) and then append that to the result (return [ ...p, c ];)

Otherwise, just ignore this one, and continue with the rest (return p);

const all = {"activeBookingsCostBreakdownGroups": [{"bookType": "RAIL", "costBreakdowns": [{"canceled": false, "totalInTripCurrency": {"amount": 43.39 } } ] }, {"bookType": "CAR", "costBreakdowns": [{"canceled": true, "totalInTripCurrency": {"amount": 86.78 } }, {"canceled": false, "totalInTripCurrency": {"amount": 87.79 } } ] } ]}

const data = all.activeBookingsCostBreakdownGroups;

const filtered = data.reduce((p, c) => {
    let withoutFalse = c.costBreakdowns.filter(c => !c.canceled);
    if (withoutFalse.length) {
        c.costBreakdowns = withoutFalse;
        return [ ...p, c ];
    }
    return p;
}, []);
    
console.log(filtered);
[
  {
    "bookType": "RAIL",
    "costBreakdowns": [
      {
        "canceled": false,
        "totalInTripCurrency": {
          "amount": 43.39
        }
      }
    ]
  },
  {
    "bookType": "CAR",
    "costBreakdowns": [
      {
        "canceled": false,
        "totalInTripCurrency": {
          "amount": 87.79
        }
      }
    ]
  }
]
0stone0
  • 34,288
  • 4
  • 39
  • 64
1

You can use Array.map() along with Array.filter() to remove all cost breakdown elements with canceled = true.

const a = {"activeBookingsCostBreakdownGroups": [ { "bookType": "RAIL", "costBreakdowns": [ { "canceled": false, "totalInTripCurrency": { "amount": 43.39 } } ] }, { "bookType": "CAR", "costBreakdowns": [ { "canceled": true, "totalInTripCurrency": { "amount": 86.78 } }, { "canceled": false, "totalInTripCurrency": { "amount": 87.79 } } ] } ]}
          
const result = { 
    activeBookingsCostBreakdownGroups: a.activeBookingsCostBreakdownGroups.map(obj => { 
        return { ...obj, costBreakdowns: obj.costBreakdowns.filter(cb => !cb.canceled) }
    })
}
 
 console.log(result)
.as-console-wrapper { max-height: 100% !important; }
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
0

this code can be use too, use with map and filter

const a = {
  activeBookingsCostBreakdownGroups: [
    {
      bookType: "RAIL",
      costBreakdowns: [
        { canceled: false, totalInTripCurrency: { amount: 43.39 } },
      ],
    },
    {
      bookType: "CAR",
      costBreakdowns: [
        { canceled: true, totalInTripCurrency: { amount: 86.78 } },
        { canceled: false, totalInTripCurrency: { amount: 87.79 } },
      ],
    },
  ],
};

const data = a.activeBookingsCostBreakdownGroups;
const filter = data.map(function (value) {
  const isNotCancel = value.costBreakdowns.filter((c) => !c?.canceled);
  return { ...value, costBreakdowns: isNotCancel };
});

console.log(filter);
appsaeed
  • 21
  • 2
0

You can use .map method with destructuring syntax and spread operator ... to extract costBreakdowns value and the rest of your object. Inside the map funtion , a new object is created using the ...rest props and results of costBreakdowns after running filter method.

const a = {
  "activeBookingsCostBreakdownGroups": [{
"bookType": "RAIL",
"costBreakdowns": [{
  "canceled": false,
  "totalInTripCurrency": {
    "amount": 43.39
  }
}, ]
  }, {
"bookType": "CAR",
"costBreakdowns": [{
    "canceled": true,
    "totalInTripCurrency": {
      "amount": 86.78
    }
  },
  {
    "canceled": false,
    "totalInTripCurrency": {
      "amount": 87.79
    }
  },
]
  }]
};

const result = {
  activeBookingsCostBreakdownGroups: a.activeBookingsCostBreakdownGroups.map(({costBreakdowns, ...rest}) => ({
...rest,
costBreakdowns: costBreakdowns.filter(({canceled}) => !canceled),
  })),
};

console.log(result);
protob
  • 3,317
  • 1
  • 8
  • 19