Home > Net >  Dynamically change data in array
Dynamically change data in array

Time:03-30

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.

CodePudding user response:

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);

CodePudding user response:

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)

CodePudding user response:

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];
    }
});

CodePudding user response:

You could use the Array.prototype.includes() method. Just extract the value from your object and use a conditional statement.

if array.includes(yourObjectDate) {
return true
} else {
return false
}

CodePudding user response:

for (const property in object) {
  array.find(date => object[property].marked = date === property);
}

CodePudding user response:

Here's how to remove the object whose date is not in dates:

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

Object.keys(objects).forEach(
    key => dates.includes(key) || delete objects[key]
); 

console.log(objects)

  • Related