Skip to main content
Tweeted twitter.com/StackCodeReview/status/1279022088280514560
deleted 173 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Is there a simpler way to replace Replace nested property? for an interview

During an interview I've gotI was given this interesting javascript-taskJavaScript task:

Given the code:

const testStr = "bar.baz.foo:111222",
      testObj = {
        bar: {
          baz: {
            foo: 333444,
            foo2: 674654
          },
          boo: {
            faa: 11
          }
        },
        fee: 333
      };

function replaceInObj(obj, str) {
  // your code
}

write the implementation of the replaceInObj function

  1. I was not allowed to use 3rd-party library.
  2. I kinda solved the task, but I'm not satisfied with my solution.
  3. And I'm struggling with my complex implementation.
  4. At the same time I cannot find another solution by myself.

I was not allowed to use a 3rd-party library. I kinda solved the task, but I'm not satisfied with my solution and I'm struggling with my complex implementation. At the same time I cannot find another solution by myself.

My implementation of the replaceInObj function

const testStr = 'bar.baz.foo:111222';
const testObj = {
  bar: {
    baz: {
      foo: 333444,
      foo2: 674654,
    },
    boo: {
      faa: 11,
    },
  },
  fee: 333,
};

console.log('[before] testObj.bar.baz =', testObj.bar.baz);

// run
replaceInObj(testObj, testStr);

console.log('[after] testObj.bar.baz =', testObj.bar.baz);

function replaceInObj(obj, str) {
  const [path, valueToChange] = str.split(':');

  path.split('.').reduce((acc, prop, index, arr) => {
    const isLastProp = arr.length === index + 1;
    if (isLastProp) {
      acc[prop] = valueToChange;
    }

    return acc[prop];
  }, obj);
}

How would you implement the function replaceInObj?

  • I hope there's much-much simpler implementation.
  • And I would be grateful if you'd share your solution

Is there a simpler way to replace nested property?

During an interview I've got interesting javascript-task:

Given the code:

const testStr = "bar.baz.foo:111222",
      testObj = {
        bar: {
          baz: {
            foo: 333444,
            foo2: 674654
          },
          boo: {
            faa: 11
          }
        },
        fee: 333
      };

function replaceInObj(obj, str) {
  // your code
}

write the implementation of the replaceInObj function

  1. I was not allowed to use 3rd-party library.
  2. I kinda solved the task, but I'm not satisfied with my solution.
  3. And I'm struggling with my complex implementation.
  4. At the same time I cannot find another solution by myself.

My implementation of the replaceInObj function

const testStr = 'bar.baz.foo:111222';
const testObj = {
  bar: {
    baz: {
      foo: 333444,
      foo2: 674654,
    },
    boo: {
      faa: 11,
    },
  },
  fee: 333,
};

console.log('[before] testObj.bar.baz =', testObj.bar.baz);

// run
replaceInObj(testObj, testStr);

console.log('[after] testObj.bar.baz =', testObj.bar.baz);

function replaceInObj(obj, str) {
  const [path, valueToChange] = str.split(':');

  path.split('.').reduce((acc, prop, index, arr) => {
    const isLastProp = arr.length === index + 1;
    if (isLastProp) {
      acc[prop] = valueToChange;
    }

    return acc[prop];
  }, obj);
}

How would you implement the function replaceInObj?

  • I hope there's much-much simpler implementation.
  • And I would be grateful if you'd share your solution

Replace nested property for an interview

During an interview I was given this interesting JavaScript task:

Given the code:

const testStr = "bar.baz.foo:111222",
      testObj = {
        bar: {
          baz: {
            foo: 333444,
            foo2: 674654
          },
          boo: {
            faa: 11
          }
        },
        fee: 333
      };

function replaceInObj(obj, str) {
  // your code
}

write the implementation of the replaceInObj function

I was not allowed to use a 3rd-party library. I kinda solved the task, but I'm not satisfied with my solution and I'm struggling with my complex implementation. At the same time I cannot find another solution by myself.

My implementation of the replaceInObj function

const testStr = 'bar.baz.foo:111222';
const testObj = {
  bar: {
    baz: {
      foo: 333444,
      foo2: 674654,
    },
    boo: {
      faa: 11,
    },
  },
  fee: 333,
};

console.log('[before] testObj.bar.baz =', testObj.bar.baz);

// run
replaceInObj(testObj, testStr);

console.log('[after] testObj.bar.baz =', testObj.bar.baz);

function replaceInObj(obj, str) {
  const [path, valueToChange] = str.split(':');

  path.split('.').reduce((acc, prop, index, arr) => {
    const isLastProp = arr.length === index + 1;
    if (isLastProp) {
      acc[prop] = valueToChange;
    }

    return acc[prop];
  }, obj);
}

Source Link
bekliev
  • 151
  • 5

Is there a simpler way to replace nested property?

During an interview I've got interesting javascript-task:

Given the code:

const testStr = "bar.baz.foo:111222",
      testObj = {
        bar: {
          baz: {
            foo: 333444,
            foo2: 674654
          },
          boo: {
            faa: 11
          }
        },
        fee: 333
      };

function replaceInObj(obj, str) {
  // your code
}

write the implementation of the replaceInObj function

  1. I was not allowed to use 3rd-party library.
  2. I kinda solved the task, but I'm not satisfied with my solution.
  3. And I'm struggling with my complex implementation.
  4. At the same time I cannot find another solution by myself.

My implementation of the replaceInObj function

const testStr = 'bar.baz.foo:111222';
const testObj = {
  bar: {
    baz: {
      foo: 333444,
      foo2: 674654,
    },
    boo: {
      faa: 11,
    },
  },
  fee: 333,
};

console.log('[before] testObj.bar.baz =', testObj.bar.baz);

// run
replaceInObj(testObj, testStr);

console.log('[after] testObj.bar.baz =', testObj.bar.baz);

function replaceInObj(obj, str) {
  const [path, valueToChange] = str.split(':');

  path.split('.').reduce((acc, prop, index, arr) => {
    const isLastProp = arr.length === index + 1;
    if (isLastProp) {
      acc[prop] = valueToChange;
    }

    return acc[prop];
  }, obj);
}

How would you implement the function replaceInObj?

  • I hope there's much-much simpler implementation.
  • And I would be grateful if you'd share your solution