How to use indexOf ()?

-2

I have the following code:

var addItem = document.getElementById("add-item");
    var formItem = document.getElementById("form-item");
    var itens = [];
    var idItem = 0;
    var idDiv = 0;

    addItem.addEventListener("click", function(){
        var nome = document.getElementById("nome-item");
        var codigo = document.getElementById("cod-item");
        var descricao = document.getElementById("desc-item");
        var qtd = document.getElementById("qtd-item");
        var valor = document.getElementById("valor-item");
        idItem++;

        adicionaItem(idItem, nome, codigo, descricao, qtd, valor);
    });
    function adicionaItem(idItem, nome, codigo, descricao, qtd, valor){
        var novoItem = {};
        novoItem['id'] = idItem;
        novoItem['nome'] = nome.value;
        novoItem['codigo'] = codigo.value;
        novoItem['descricao'] = descricao.value;
        novoItem['quantidade'] = qtd.value;
        novoItem['valor'] = valor.value;

        itens.push(novoItem);

        mostraItens(itens, novoItem);
        console.log(idItem);
    }
    function mostraItens(itens, novoItem){

        var tagLinha = document.createElement("DIV");
        tagLinha.className = "row";
        tagLinha.setAttribute("id", idDiv);
        formItem.appendChild(tagLinha);

        var botaoDel = document.createElement("BUTTON");
        botaoDel.setAttribute("type", "button");
        botaoDel.setAttribute("onclick", "removeItem(itens)");
        botaoDel.textContent = "Excluir";
        tagLinha.appendChild(botaoDel);

        idDiv++;
    }
    function removeItem(itens){

        console.log(itens.indexOf());

    }

In the removeItem function log I would like to find the index of the object with a specific id, but I do not know how to do it. I would like to know how to use indexOf () correctly.

    
asked by anonymous 15.10.2018 / 14:13

1 answer

2

To remove an element from a array by index, you must use the splice ". An example:

const arr = ['A', 'B', 'C', 'D', 'E'];
arr.splice(3, 1); // Remove o elemento de índice 3 ('D')
console.log(arr);

To find the index in a array of objects, does not use indexOf . The most recommended method is findIndex , which accepts a function as an argument so that you can select the element that you want to find the index.

The operation of findIndex is simple: it will run over all elements of array , passing the element of each iteration as a parameter to the function passed as argument.

If the argument function returns true , the other iterations are stopped, and the findIndex method returns the index of the element. It is also important to keep in mind that if no element meets the condition, returning true , findIndex will return -1 .

For example to make it less confusing:

const shopList = [{
  id: 1,
  item: 'Maçã'
}, {
  id: 2,
  item: 'Pêra'
}, {
  id: 3,
  item: 'Banana'
}, {
  id: 4,
  item: 'Café'
}];

// Achar o elemento que tem o ID 3:
const index = shopList.findIndex(function (shopItem) {
  // Realizamos a busca:
  // 'findIndex' retornará o índice do primeiro elemento cuja iteração retornar 'true':
  if (shopItem.id === 3) {
    return true;
  }
  
  return false;
});

console.log(index);
console.log(shopList[index]);

// Remover:
shopList.splice(index, 1);
console.log(shopList);

For instructional reasons, I tried to make the above code simpler to understand. Keep in mind, however, that it can be greatly improved:

const shopList = [{
  id: 1,
  item: 'Maçã'
}, {
  id: 2,
  item: 'Pêra'
}, {
  id: 3,
  item: 'Banana'
}, {
  id: 4,
  item: 'Café'
}];

const index = shopList.findIndex(({ id }) => id === 3);
shopList.splice(index, 1);
console.log(shopList);

Reference:

15.10.2018 / 14:41