Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading