Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
How to remove elements from object of arrays

I use an object of arrays and I want to delete a specific value in 1 of the arrays.

let medici= ["Person1","Person2", "Person3", "Person4", "Person5",  "Person6" ];
let giorni = ["Lun", "Mar", "Mer","Gio","Ven"]; let presenti ={};
for  (let giorno of giorni){ presenti[giorno] = medici; }

I obtain:

Gio: ["Person1", "Person2", "Person3", "Person4", "Person5", "Person6"],
  Lun: ["Person1", "Person2", "Person3", "Person4", "Person5", "Person6"],
  Mar: ["Person1", "Person2", "Person3", "Person4", "Person5", "Person6"],
  Mer: ["Person1", "Person2", "Person3", "Person4", "Person5", "Person6"],
  Ven: ["Person1", "Person2", "Person3", "Person4", "Person5", "Person6"]

Now I want to delete specific value in specific array:

giorno="Ven";
nome="Person1";
presenti[giorno].splice(presenti[giorno].indexOf(nome), 1);

It delete the value in all the arrays!!! Not only in the "Ven" one... How can I delete a single one element in a specific array not affecting the other arrays of the object?

Thx

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

const medici = ["Person1","Person2", "Person3", "Person4", "Person5",  "Person6" ];
const gironi = ["Lun", "Mar", "Mer","Gio","Ven"];
const presenti = {};

for (const giorno of gironi) {
    presenti[giorno] = [...medici];
}

presenti["Ven"].splice(presenti["Ven"].indexOf("Person1"), 1);

console.log(presenti);

Your problem has to do with memory reference, when you equate two variables you are not creating another identical object but copying the object reference from one variable to another, so if you modify X you change Y, to correct you can create another Array using the spread operator [...medici]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

about 4 years ago · Juan Pablo Isaza Denunciar

0

When you build your object all the arrays have the same reference so you will need to slice medici on each object value. Take a look to Copy array by value

Now you can use method.

let medici= ["Person1","Person2", "Person3", "Person4", "Person5",  "Person6" ];
let giorni = ["Lun", "Mar", "Mer","Gio","Ven"]; let presenti ={};
for  (let giorno of giorni){ presenti[giorno] = medici.slice(); }

let giorno="Ven";
let nome="Person1";

presenti[giorno].splice(presenti[giorno].indexOf(nome), 1);
console.log(presenti)

about 4 years ago · Juan Pablo Isaza Denunciar

0

let medici= ["Person1","Person2", "Person3", "Person4", "Person5",  "Person6" ];
let giorni = ["Lun", "Mar", "Mer","Gio","Ven"]; let presenti ={};
for  (let giorno of giorni){ presenti[giorno] = medici; }

giorno="Ven";
nome="Person1";

const filteredData = presenti[giorno].filter((p)=>p!==nome)

presenti[giorno] = filteredData

console.log(presenti)

It does so because in JavaScript Array and Objects are known as reference types. [Learn more here].1

To fix your problem you can do this:

giorno="Ven";

nome="Person1";

At first filter out Person1 from the array. const filteredData = presenti[giorno].filter((p)=>p!==nome)

Then replace the Ven object with the updated filteredData

presenti[giorno] = filteredData

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda