2

I am trying to update some values ​​of an object with conditionals, is there a way to do it without iterating all the elements?

This code works but I want to do the same more efficiently with a query, search method or something like that

const testObj = () =>{
  let objAttrDay = {
   "1":{
      "sunday":false,
      "recurrentSunday":false
   },
   "2":{
      "sunday":true,
      "recurrentSunday":false
   },
   "3":{
      "sunday":true,
      "recurrentSunday":false
   }
};
let contSunday = 3;
if(contSunday >= 3){
for(let i in objAttrDay){
    if(objAttrDay[i].sunday){
        objAttrDay[i].recurrentSunday = true;
    }
}
}
console.log(objAttrDay);
}
testObj();
Julian Benavides
  • 320
  • 1
  • 11

3 Answers3

1

Object.entries breaks down an object into an array of key, value pairs. Then you can easily map that array to another array with the recurrentSunday property set how you want. Then recreate your object using Object.fromEntries. You will need a browser that supports entries and fromEntries, which were introduced in ECMAScript 2019.

const testObj = () => {
  let objAttrDay = {
    "1": {
      "sunday": false,
      "recurrentSunday": false
    },
    "2": {
      "sunday": true,
      "recurrentSunday": false
    },
    "3": {
      "sunday": true,
      "recurrentSunday": false
    }
  };
  let contSunday = 3;
  if (contSunday >= 3) {
    let entries = Object.entries(objAttrDay);
    entries = entries.map(([key, value]) => {
      return [key, { 
        ...value,
        recurrentSunday: value.sunday
      }];
    });
    objAttrDay = Object.fromEntries(entries);
  }
  console.log(objAttrDay);
}
testObj();
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
0

There are two fairly clean ways of replacing values in JS objects and arrays that I can think of.

Option 1 - create a new Object

Let's assume that we have an object like the one you defined above.

    let objAttrDay = {
      "1":{
        "sunday":false,
        "recurrentSunday":false
      },
      "2":{
        "sunday":true,
        "recurrentSunday":false
      },
      "3":{
        "sunday":true,
        "recurrentSunday":false
      }
    };

we can change the value of "recurrentSunday" to true like this.

    objAttrDay = {
           ...objAttrDay, // deconstruct all previous values from this object
           "3": {
                ...objAttrDay["3"], // we could also just write `"sunday": true`
                "recurrentSunday":true // change value to true
            }
          };

You'll find that the only change made to the objAttrDay Object is the value of objAttrDay["3"]["recurrentSunday"]

Option 2 - Change the value of a specific key in your existing Object

Alternatively, we can select only the value that we want to change, like so:

objAttrDay["3"]["recurrentSunday"] = true

The first option is useful if you would like to create a new object to store your values which is popular in reactjs and often necessary if the initial object is immutable. (Like state or props).

If, however, you don't need to create a new object, I would recommend the second method as it is short, succinct, and will likely save processing power.

Example of your if statement with the second option.

if(contSunday >= 3){
   objAttrDay["3"]["recurrentSunday"] = true;
};

Hope this helped!

Shriever
  • 36
  • 2
0

Maybe something like this:

let objAttrDay = {
  "1": {
    "sunday": false,
    "recurrentSunday": false
  },
  "2": {
    "sunday": true,
    "recurrentSunday": false
  },
  "3": {
    "sunday": true,
    "recurrentSunday": false
  }
}

const updateObject = (contSunday, object) => 
    Object.keys(object)
    .filter((key) => key >= contSunday)
    .map((key) => { return {key, ...object[key]}})
    .filter((item) => item.sunday)
    .map(({recurrentSunday, ...i})=> { return {recurrentSunday:true, ...i}})
    .map(({key, ...item})=> {object[key] = item; return object})


let result = updateObject(3, objAttrDay)
console.log(result);
NeNaD
  • 18,172
  • 8
  • 47
  • 89