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

247
Views
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 answers
Answer question

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 Report

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 Report

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 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!