Home > front end >  Remove element from Array with condition
Remove element from Array with condition

Time:06-09

I have an array of objects which looks like the following

    [
  {
    year: 2023,
    total: 0,
    transfer: -13,
    totalWithTransfer: -13,
    takenHolidays: 0,
    result: 24,
    vacationAfterToday: 0,
    vacationBeforeToday: 0
  },
  {
    year: 2022,
    total: 0,
    transfer: -4,
    totalWithTransfer: -4,
    takenHolidays: 9,
    result: 27,
    vacationAfterToday: 3,
    vacationBeforeToday: 6
  },
  {
    year: 2021,
    total: 0,
    transfer: 0,
    totalWithTransfer: 0,
    takenHolidays: 4,
    result: 28,
    vacationAfterToday: 0,
    vacationBeforeToday: 4
  },
  {
    year: 2020,
    total: 0,
    transfer: 0,
    totalWithTransfer: 0,
    takenHolidays: 0,
    result: 24,
    vacationAfterToday: 0,
    vacationBeforeToday: 0
  }
]

I want a condition which checks if the total from the first object is zero, and if yes this object should be deleted from the array. Greetings, Leo

CodePudding user response:

// For checking & removing first object only
       var a= [
      {
        year: 2023,
        total: 10,
        transfer: -13,
        totalWithTransfer: -13,
        takenHolidays: 0,
        result: 24,
        vacationAfterToday: 0,
        vacationBeforeToday: 0
      },
      {
        year: 2022,
        total: 0,
        transfer: -4,
        totalWithTransfer: -4,
        takenHolidays: 9,
        result: 27,
        vacationAfterToday: 3,
        vacationBeforeToday: 6
      },
      {
        year: 2021,
        total: 0,
        transfer: 0,
        totalWithTransfer: 0,
        takenHolidays: 4,
        result: 28,
        vacationAfterToday: 0,
        vacationBeforeToday: 4
      },
      {
        year: 2020,
        total: 0,
        transfer: 0,
        totalWithTransfer: 0,
        takenHolidays: 0,
        result: 24,
        vacationAfterToday: 0,
        vacationBeforeToday: 0
      }
    ]
    
    if(a[0].total==0)
    {
        a.splice(0,1);
    }

console.log(a)

CodePudding user response:

You can simply check if the first element exists and then use shift method

const array = [
      {
        year: 2023,
        total: 10,
        transfer: -13,
        totalWithTransfer: -13,
        takenHolidays: 0,
        result: 24,
        vacationAfterToday: 0,
        vacationBeforeToday: 0
      },
      {
        year: 2022,
        total: 0,
        transfer: -4,
        totalWithTransfer: -4,
        takenHolidays: 9,
        result: 27,
        vacationAfterToday: 3,
        vacationBeforeToday: 6
      },
      {
        year: 2021,
        total: 0,
        transfer: 0,
        totalWithTransfer: 0,
        takenHolidays: 4,
        result: 28,
        vacationAfterToday: 0,
        vacationBeforeToday: 4
      },
      {
        year: 2020,
        total: 0,
        transfer: 0,
        totalWithTransfer: 0,
        takenHolidays: 0,
        result: 24,
        vacationAfterToday: 0,
        vacationBeforeToday: 0
      }
    ]
    
    if (array[0].total === 0) {
      array.shift();
    }
    
    console.log(array)

  • Related