#reactjs #typescript #object
#reactjs #typescript #объект
Вопрос:
Я пытался разобраться в этом в течение нескольких дней и ничего не получал.
У меня есть массив объектов в ReactJS, в котором я пытаюсь отредактировать элемент одного из элементов в массиве:
Структура объекта выглядит следующим образом:
actor: null
aggregate: "Comment"
aggregateId: "5fcf206131414a05bcee9f88"
createdAt: "2020-12-08T06:42:41.983Z"
data: {…}
comment: {…}
author: Object { id: "5ea683231fac57758ae7e4e2", nodeId: "MDQ6VXNlcjI1MTc0ODQ=", username: "monstertrux", … }
body: "ths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfnnths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfnnths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdfths is an edit testsfsdfsdf"
createdAt: "2020-12-08T06:42:41.983Z"
id: "5fcf206131414a05bcee9f88"
topicId: "5f3e3eb92db37843068c045a"
updatedAt: "2020-12-09T10:32:05.889Z"
<prototype>: Object { … }
<prototype>: Object { … }
event: null
id: "5fcf206131414a05bcee9f88"
message: null```
have been trying to navigate the array and then edit the comment field with the following:
пусть itemToEdit = tempTimeline[foundIndex]
console.log(itemToEdit)
itemToEdit = {...itemToEdit, data.comment.body: comment }
which pulls the item to be edited from the array and then tries to edit the data.comment.body element in the hierarchy.
But all I get for my efforts is the following errors:
No value exists in scope for the shorthand property 'data'. Either declare one or provide an initializer.
',' expected.
Type '{ data: any; "": any; comment: string; id?: string | undefined; aggregateId?: string | undefined; aggregate?: string | undefined; event?: string | null | undefined; message?: string | null | undefined; actor?: Member | ... 1 more ... | undefined; createdAt?: string | undefined; }' is not assignable to type 'IssueTimeline'.
Object literal may only specify known properties, and '(Missing)' does not exist in type 'IssueTimeline'.
',' expected.
Anyone have any idea what I am doing wrong here?
Ответ №1:
Вы не можете указать ключи с точечной нотацией, вам нужно будет пройти через уровни следующим образом:
const yourArray = [...];
const newArray = yourArray.map((item, idx) => {
if (idx === indexToEdit) {
return {
...item,
data: {
...item.data,
comment: {
...item.data.comment,
body: comment,
}
}
};
}
return item;
});
Комментарии:
1. спасибо за ответ, я действительно пробовал этот подход раньше и получаю сообщение об ошибке в …item.data.comment объекта, возможно, ‘undefined’. Похоже, та же проблема
2. Вы могли бы использовать нулевой оператор объединения для обработки этого:
...(item.data.comment ?? {})