0

I have a select and rowData[] when I click on the Add button, I add the name and the value on a table where I use the function .push for add the data into a JSON array, but I don't want to add duplicates values.

 addItem () {

    var equipos_torneo = {
      id_torneo: this.id_torneo,
      id_equipo: this.id_equipo,
      nombre_equipo: nombre_team,
    }

    this.rowData.push(equipos_torneo)
    console.log(equipos_torneo.id_equipo)
  },

} 

Table:

   <tbody>
        <tr
          v-for="item in rowData"
          :key="item.id_torneo"
        >
          <td>{{ item.id_torneo }}</td>
          <td>{{ item.id_equipo }}</td>
          <td>{{ item.nombre_equipo }}</td>
        </tr>
      </tbody>
4
  • Which property do you want to be unique in the array? Is it id_torneo? Or another, or all of them?
    – danh
    Commented Aug 13, 2021 at 18:54
  • @danh In that case, id_equipo
    – Rodrigo
    Commented Aug 13, 2021 at 18:56
  • use .some() Commented Aug 13, 2021 at 18:58
  • You can use this.rowData.findIndex(e => e.id_equipo === equipos_torneo.id_equipo) in order to find out if an array already has an object in it with the same id_equipo property. If this function returns -1, then you can push the new item. Commented Aug 13, 2021 at 18:58

2 Answers 2

1

I think I understand the question to be asking how to prevent a push of an object that has duplicate value(s) in the array. Just find out (with find) before pushing.

addItem() {
  let equipos_torneo = {
    id_torneo: this.id_torneo,
    id_equipo: this.id_equipo,
    nombre_equipo: nombre_team,
  };
  let exists = this.rowData.find(el => el.id_equipo === equipos_torneo.id_equipo);
  if (!exists) this.rowData.push(equipos_torneo);
}
3
  • Semantically, Array.prototype.some() would be a better choice here, but otherwise that is the right answer.
    – aweebit
    Commented Aug 13, 2021 at 19:06
  • @aweebit, aren't the semantics equivalent? If there aren't some then there isn't one. If there isn't one, then there aren't some.
    – danh
    Commented Aug 13, 2021 at 19:10
  • the code works the same, I just meant that some() is exactly there for the purpose of checking if an item satisfying certain criteria exists in an array, and so it is better suited for this use case by its meaning. Maybe I used the word
    – aweebit
    Commented Aug 13, 2021 at 19:45
1

You need to check whether there is any similar value before pushing to rowData[].

I suggest something like this:

addItem(){
let duplicates=[]
var equipos_torneo = {
  id_torneo: this.id_torneo,
  id_equipo: this.id_equipo,
  nombre_equipo: nombre_team,
}
this.rowData.forEach((element, index)=>{
    if(element.id_torneo===equipos_torneo.id_torneo){
        duplicates.push(index)
  }
    else if(element.id_equipo===equipos_torneo.id_equipo){
        duplicates.push(index)
  }
    else if(element.nombre_equipo===equipos_torneo.nombre_equipo){
        duplicates.push(index)
  }
    else{
        this.rowData.push(equipos_torneo)
  }
})
}

this way you only push to rowData[] only if there are no duplicated. If there are any duplicates, you will find them in duplicates[]

Not the answer you're looking for? Browse other questions tagged or ask your own question.