Home > database >  How can i remove one object from object array using vuejs
How can i remove one object from object array using vuejs

Time:04-12

I have an object of object array in json format.I want to remove one object form this object array using vuejs.

vuejs

export default {
  components: { leftmenu, countDown, timer, Header },
  data() {
    return {
      this.orders = {"data":{"175":{"details":[{"order_id":175,"item_id":1,"item_name":"pizza"},{"order_id":175,"item_id":2,"item_name":"burger"}]},"173":{"details":[{"order_id":175,"item_id":1,"item_name":"pizza"}]}}}
    };
  },
}

I have tried to remove object which have key 175 using below code.But did not work.

        const filtersList = Object.keys(this.orders.data).filter(
         (element) => element !== index
       );
       this.orders = filerslist

CodePudding user response:

You can use the delete keyword (documentation here) to remove keys from objects

const orders = {
  "data": {
    "175": {
      "details": [{
        "order_id": 175,
        "item_id": 1,
        "item_name": "pizza"
      }, {
        "order_id": 175,
        "item_id": 2,
        "item_name": "burger"
      }]
    },
    "173": {
      "details": [{
        "order_id": 175,
        "item_id": 1,
        "item_name": "pizza"
      }]
    }
  }
}

delete orders.data["175"]
console.log(orders)

  • Related