Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
Dynamically change data in array

I have an array

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",
]

And I have the data of objects:

 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,
  },
}

In the objects, theres is a date "2022-03-27", however, it is not in the array. What I would like to do is, if the date is not in the ARRAY, I would like to remove it from the object, or mark it as false.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

To avoid mutating the original object you can use Object.fromEntries passing the filtered Object.entries of the original. Here simply using filter() and testing using 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);

To instead toggle the marked properties of each nested object you can use a similar method, but instead of filter() you can map() the original Object.entries here using spread syntax to clone each nested object and overwrite the marked property with the result of the same includes() call as the previous example.

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 Relatório

0

I'm taking all the keys of your object, then iterating over the collection and updating the marked property according to whether the date is included in the Array.
Assumption: The objects Object is being used to track state and so is intended to be mutable.

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)

In order to avoid updating objects that have no meaningful change, you could do this instead:

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

0

Iterate over object keys, find if the key exists in the array, delete it from the object if not found:

Object.keys(obj).forEach(function(key) {
    if (arr.includes(key) === false) {
        delete obj[key];
    }
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda