1

Help pls.

We have object:

let obj = {
hasError: {
    income_manager: {
      signatory_id: false,
    }
  },
};

and string:

let halfObject = "income_manager.signatory_id";

how can i splice a string with objects to write a new value?

example:

let variable = halfObject.split('.')

obj.hasError[variable[0]][variable[1]] = true;

But it doesn't look good. Do you have a prettier example?

nbsp
  • 11
  • 2
  • what does "not good" mean? Looks like it should work fine to me, if you replace `that` with `obj`. (No idea where `that` comes from.) – Robin Zigmond Jan 12 '22 at 22:37
  • Perhaps you would prefer destructuring assignment, for example: `let [a, p] = e.split('.'); c.r[a][p] = true`. – MikeM Jan 12 '22 at 22:53

1 Answers1

0

Try this:

let obj = {
  hasError: {
    income_manager: {
      signatory_id: false
    }
  }
};

let halfObject = "income_manager.signatory_id";

let variable = halfObject.split('.');

obj.hasError[variable[0]][variable[1]] = true;

console.log(obj.hasError[variable[0]][variable[1]]);
Eric
  • 488
  • 1
  • 3
  • 10