Multiple Keys/values in a javascript array

Asked

Viewed 222 times

1

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

The problem is that it is overwriting the ids with the last 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 cannot use a form because of layout layout

  • I didn’t understand your doubt, could you explain better which part of the code you have doubt? And what exactly do you want to do?

  • Ids are unique Each element can have only one ID; Each page can have only one element with that ID. You should reformulate your html code so as not to use id.

  • 1

    @Thiagoyoithi changed the question, see if you can understand now

1 answer

1


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

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

Would that be 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 think about it, I believe that the "objs" would have the same value as this.ids, already gave a console.log(this.ids); within its massDelete function()?

  • 1

    I pass another parameter on the massDelete(Parametro) When the guy clicks on the boot, but this then helped me, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.