JSFiddle - React, Tailwind, and code Playground

by Sudheer Nunna

JavaScript

const setNestedProp = (obj = {}, [first, ...rest] , value) => ({
  ...obj,
  [first]: rest.length
    ? setNestedProp(obj[first], rest, value)
    : value
});

const result = {};
const result2 = setNestedProp(result, ["first", "second", "b"], "bar");
console.log(result2)