Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
Cambiar dinámicamente los datos en la matriz

tengo una matriz

 Array [ "2022-03-25", "2022-03-10", "2022-03-24", "2022-03-31", "2022-03-12", "2022-03-23", "2022-03-22", "2022-03-16", "2022-03-11", "2022-03-26", ]

Y tengo los datos de los objetos:

 Object { "2022-03-10": Object { "marked": true, }, "2022-03-11": Object { "marked": true, }, "2022-03-12": Object { "marked": true, }, "2022-03-16": Object { "marked": true, }, "2022-03-18": Object { "marked": true, }, "2022-03-22": Object { "marked": true, }, "2022-03-23": Object { "marked": true, }, "2022-03-24": Object { "marked": true, }, "2022-03-25": Object { "marked": true, }, "2022-03-26": Object { "marked": true, }, "2022-03-27": Object { "marked": true, }, "2022-03-31": Object { "marked": true, }, }

En los objetos, hay una fecha "2022-03-27", sin embargo, no está en la matriz. Lo que me gustaría hacer es, si la fecha no está en ARRAY, me gustaría eliminarla del objeto o marcarla como falsa.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Para evitar la mutación del objeto original, puede usar Object.fromEntries pasando las Object.entries filtradas del original. Aquí simplemente usando filter() y probando usando includes() .

 const array = ["2022-03-25", "2022-03-10", "2022-03-24", "2022-03-31", "2022-03-12", "2022-03-23", "2022-03-22", "2022-03-16", "2022-03-11", "2022-03-26",]; const object = { "2022-03-10": { "marked": true, }, "2022-03-11": { "marked": true, }, "2022-03-12": { "marked": true, }, "2022-03-16": { "marked": true, }, "2022-03-18": { "marked": true, }, "2022-03-22": { "marked": true, }, "2022-03-23": { "marked": true, }, "2022-03-24": { "marked": true, }, "2022-03-25": { "marked": true, }, "2022-03-26": { "marked": true, }, "2022-03-27": { "marked": true, }, "2022-03-31": { "marked": true, }, }; const filteredObject = Object.fromEntries( Object.entries(object) .filter(([key]) => array.includes(key)) ); console.log(filteredObject);

En su lugar, para alternar las propiedades marked de cada objeto anidado, puede usar un método similar, pero en lugar de filter() , puede map() el objeto original Object.entries aquí usan la sintaxis extendida para clonar cada objeto anidado y sobrescribir la propiedad marked con el resultado. de la misma llamada includes() que en el ejemplo anterior.

 const array = ["2022-03-25", "2022-03-10", "2022-03-24", "2022-03-31", "2022-03-12", "2022-03-23", "2022-03-22", "2022-03-16", "2022-03-11", "2022-03-26",]; const object = { "2022-03-10": { "marked": true, }, "2022-03-11": { "marked": true, }, "2022-03-12": { "marked": true, }, "2022-03-16": { "marked": true, }, "2022-03-18": { "marked": true, }, "2022-03-22": { "marked": true, }, "2022-03-23": { "marked": true, }, "2022-03-24": { "marked": true, }, "2022-03-25": { "marked": true, }, "2022-03-26": { "marked": true, }, "2022-03-27": { "marked": true, }, "2022-03-31": { "marked": true, }, }; const markedObject = Object.fromEntries( Object.entries(object) .map(([key, value]) => [key, { ...value, marked: array.includes(key) }]) ); console.log(markedObject);

about 4 years ago · Juan Pablo Isaza Report

0

Estoy tomando todas las claves de su objeto, luego itero sobre la colección y actualizo la propiedad marked de acuerdo con si la fecha está incluida en el Array.
Suposición: los objects Object se utilizan para rastrear el estado y, por lo tanto, están destinados a ser mutables.

 let dates = [ "2022-03-25", "2022-03-10", "2022-03-24", "2022-03-31", "2022-03-12", "2022-03-23", "2022-03-22", "2022-03-16", "2022-03-11", "2022-03-26" ] let objects = { "2022-03-10": { marked: true }, "2022-03-11": { marked: true }, "2022-03-12": { marked: true }, "2022-03-16": { marked: true }, "2022-03-18": { marked: true }, "2022-03-22": { marked: true }, "2022-03-23": { marked: true }, "2022-03-24": { marked: true }, "2022-03-25": { marked: true }, "2022-03-26": { marked: true }, "2022-03-27": { marked: true }, "2022-03-31": { marked: true } } keySet = Object.keys(objects) keySet.forEach( key => objects[ key ].marked = dates.includes(key) ) console.log(objects)

Para evitar actualizar objetos que no tienen cambios significativos, podría hacer esto en su lugar:

 Object.keys(objects).filter(date => !dates.includes(date)).map( date => objects[ date ].marked = false ) console.log(objects)
about 4 years ago · Juan Pablo Isaza Report

0

Itere sobre las claves del objeto, encuentre si la clave existe en la matriz, elimínela del objeto si no se encuentra:

 Object.keys(obj).forEach(function(key) { if (arr.includes(key) === false) { delete obj[key]; } });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!