Необходимо получить параметрически доступ к свойству объекта(для добавления свойства в state React), по ключам которые лежат в массиве

Рейтинг: -2Ответов: 1Опубликовано: 14.01.2023

Есть такой объект:

const commentsTestObject = {
  0: {
    path: '0',
    comment: 'lfkdlknd',
    author: 'MAX',
    subComments: {
      0: {
        path: '0.0',
        comment: 'dog',
        author: 'DOG',
        subComments: {},
      },
      1: {
        path: '0.1',
        comment: 'cat',
        author: 'Cat',
        subComments: {
          0: {
            path: '0.1.0',
            comment: 'sub',
            author: 'sub',
            subComments: {},
          }
        },
      },
      2: {
        path: '0.2',
        comment: 'bar',
        author: 'bar',
        subComments: {},
      }
    }
  },
  1: {
    path: '1',
    comment: 'foo',
    author: 'foo',
    subComments: {},
  },
  2: {
    path: '2',
    comment: 'baz',
    author: 'baz',
    subComments: {
      0: {
        path: '2.0',
        comment: 'eatmyshorts',
        author: 'eatmyshorts',
        subComments: {
          0: {
            path: '2.0.0',
            comment: 'last',
            author: 'last',
            subComments: {},
          }
        },
      },
    },
  },
};

Ко мне приходит новый subComment у которого есть свойство path: '2.0.0', соответственно мне нужно добавить его в commentsState[2][0][0].subComments, таким образом:

commentState[2][0][0].subComments = {...commentState[2][0][0].subComments, newComment}

Но всегда непонятно какой это будет path по длине (по количеству ключей).

Какую можно сделать функцию, чтобы по сути из массива [2, 0, 0] добавляли необходимый путь к свойству объекта?

Ответы

▲ 0

Предложу такой вариант...

const obj = {
  0: {
    path: '0',
    comment: 'lfkdlknd',
    author: 'MAX',
    subComments: {
      0: {
        path: '0.0',
        comment: 'dog',
        author: 'DOG',
        subComments: {},
      },
      1: {
        path: '0.1',
        comment: 'cat',
        author: 'Cat',
        subComments: {
          0: {
            path: '0.1.0',
            comment: 'sub',
            author: 'sub',
            subComments: {},
          }
        },
      },
      2: {
        path: '0.2',
        comment: 'bar',
        author: 'bar',
        subComments: {},
      }
    }
  },
  1: {
    path: '1',
    comment: 'foo',
    author: 'foo',
    subComments: {},
  },
  2: {
    path: '2',
    comment: 'baz',
    author: 'baz',
    subComments: {
      0: {
        path: '2.0',
        comment: 'eatmyshorts',
        author: 'eatmyshorts',
        subComments: {
          0: {
            path: '2.0.0',
            comment: 'last',
            author: 'last',
            subComments: {},
          }
        },
      },
    },
  },
};

const path = '2.0.0'
const o = {
  path: '2.0.0',
  comment: 'last1',
  author: 'last1',
  subComments: {},
}
test(path, o)

//
function test(path, comm) {
  const a = path.split('.')
  let o = a.reduce((o, e) => o[e].subComments, obj)
  o.subComments = {...o.subComments, comm}
  console.log(obj)
}