Multiple keys / values in a javascript array

1

In a certain part of my code I need to make a request using javascript with several identical ids.

The problem is that it is overwriting the ids with the last one selected.

My Html:

<button class="btn btn-default border-none" v-on:click="massDelete()" style="width: 100%">
    Apagar
</button>


<td class="text-center">
       <input type="checkbox" v-on:click="pushId(property.id)">
</td>

My java script;

pushId(propertyid)
        {
            this.ids.push({property_id: propertyid});
        },
massDelete()
{
  var obj = {};
  for(let item of this.ids)
            {
                obj = {property_id: item.property_id};
            }
            console.log(obj);

        }
    }

I need the answer to be

array({
"property_id": 3
}
{
"property_id": 4
}
{
"property_id": 5
})

PS: I can not use a form because of layout layout

    
asked by anonymous 06.10.2016 / 16:26

1 answer

1

An array is [], an object is {}. I believe that what you really want is this format:

[
   {
      "property_id": 3
   },
   {
      "property_id": 4
   },
   {
      "property_id": 5
   }
]

That would be it from here:

pushId(propertyid){
   this.ids.push({property_id: propertyid});
},
massDelete(){
   var objs = [];
   for(let item of this.ids){
      objs.push({property_id: item.property_id});
   }
   console.log(objs);
}

But if you stop to think, I believe that the "objs" would have the same value of this.ids, already gave a console.log (this.ids); inside your massDelete () function?

    
06.10.2016 / 18:28