How to remove specific element from an array of objects? Javascript [duplicated]

3

Good headers, I have a problem, I hope you can guide me, in what way could you eliminate a specific element of an arrangement of objects like the following?

array = [
{nom: "NOMBRE 1", tipo: "TIPO A"},
{nom: "NOMBRE 2", tipo: "TIPO B"},
{nom: "NOMBRE 3", tipo: "TIPO C"},
{nom: "NOMBRE 4", tipo: "TIPO D"},
{nom: "NOMBRE 5", tipo: "TIPO E"},
{nom: "NOMBRE 6", tipo: "TIPO F"}
];

I tried to do it with this function but I really did not succeed

var borrar = { "nom":""+"NOMBRE 2"+"","tipo":""+"TIPO B"+"" };

array  = array.filter(function(i) {
 return i !== borrar
});
    
asked by Elleiton 19.12.2017 в 13:33
source

3 answers

4

The problem is comparing objects using == , === or !== :

let obj1={a:1};
let obj2={a:1};
let obj3=obj1;

console.log(obj1==obj2);    // No son el mismo objeto, por tanto la comparación devuelve false
console.log(obj1==obj3);    // Es el mismo objeto
console.log(obj1.a==obj2.a) // Trabajando con tipos primitivos no existe el problema

Therefore, the solution to your problem is the following:

let array = [
{nom: "NOMBRE 1", tipo: "TIPO A"},
{nom: "NOMBRE 2", tipo: "TIPO B"},
{nom: "NOMBRE 3", tipo: "TIPO C"},
{nom: "NOMBRE 4", tipo: "TIPO D"},
{nom: "NOMBRE 5", tipo: "TIPO E"},
{nom: "NOMBRE 6", tipo: "TIPO F"}
];

function elimina(array,elem) {
  return array.filter(e=> e.nom!==elem.nom && e.tipo!==elem.tipo);
}


console.log(elimina(array,{nom:'NOMBRE 1',tipo : 'TIPO A'}));
    
answered by 19.12.2017 / 13:59
source
0

You could do it this way:

var idexToDelete;
    list.filter( (item, index) => {
                if (item.nom === "NOMBRE 2") {
                  if (item.tipo === "TIPO B"){
                     indexToDelete = index;
                  }
                }
    });
list.splice(indexToDelete, 1 );

Or take it to a function which would be more optimal, in my opinion.

deleteElement(list,elementToDelete){
    var idexToDelete;
    list.filter( (item, index) => {
       if (item.nom === elementoToDelete.nom) {
         if (item.tipo === elementoToDelete.tipo){
          indexToDelete = index;
         }
        }
      });
    list.splice(indexToDelete, 1 );
  }

The lodge could be re-used to obtain the index for another treatment that you want to do to the particular data.

    
answered by 19.12.2017 в 14:13
0

One option is to eliminate filtering and only obtaining the items that are different from the past to eliminate that we use the function filter that returns only the items that meet the condition that in this case is different from the past in the variable cualEliminar I hope you help greetings.

Summary

  

The filter () method creates a new array with all the elements that meet the condition implemented by the given function.

Syntax

arr.filter(callback[, thisArg])

Description

  

filter calls the given callback function for each element of the array, and constructs a new array with all the values for which callback returns a true value. callback is invoked only for array indexes that have a value assigned to it. It is not invoked for indexes that have been deleted or that have not been assigned any value. The elements of the array that do not meet the callback condition simply skip them, and they are not included in the new array.

Functional Example

array = [
{nom: "NOMBRE 1", tipo: "TIPO A"},
{nom: "NOMBRE 2", tipo: "TIPO B"},
{nom: "NOMBRE 3", tipo: "TIPO C"},
{nom: "NOMBRE 4", tipo: "TIPO D"},
{nom: "NOMBRE 5", tipo: "TIPO E"},
{nom: "NOMBRE 6", tipo: "TIPO F"}
];


var cualEliminar = {nom: "NOMBRE 4", tipo: "TIPO D"}

var array = array.filter((item)=>{
  return item.nom != cualEliminar.nom && item.tipo != cualEliminar.tipo
});

console.log(array)
    
answered by 19.12.2017 в 14:34