英文:
Result showing [object Object], [object Object] in React JS
问题
当我点击删除按钮时,会出现一个提示。提示正常工作,但不知何故显示了奇怪的 [object Object], [object Object]。我希望当提示出现时,它引用对象中的 name
。例如,当我点击删除按钮时,提示将在浏览器上显示如下:
你确定要删除 jabss 吗?
但现在它显示为:
你确定要删除 [object Object],[object Object],[object Object],[object Object] 吗?
这是删除按钮的逻辑和它在代码中的使用方式。我该如何修复这个问题?
const handleDelete = (id) => {
const newPerson = persons.filter((person) => person.id !== id)
if (window.confirm(`你确定要删除 ${newPerson} 吗?`)) {
personService
.delete(id)
setPersons(newPerson)
}
}
它在 Person
组件下面使用,如下:
<ul>
{persons.filter((person) => {
if (searchTerm === "") {
return person
} else if (person.name.toLowerCase().includes(searchTerm.toLowerCase())) {
return person
}
}).map((person, id) => {
return (
<Person key={person.id} person={person} deleteButton={() => handleDelete(person.id)} text='删除' />
);
})}
</ul>
数据存储在 db.json
中,如下:
{
"persons": [
{
"name": "Arto Hellas",
"number": "040-123456",
"id": 1
},
{
"name": "Ada Lovelace",
"number": "39-44-5323523",
"id": 2
},
{
"name": "jabss",
"number": "21321312321",
"id": 4
},
{
"name": "kamal",
"number": "231321321321",
"id": 5
},
{
"name": "jabri juhinin",
"number": "321321321321",
"id": 6
}
]
}
英文:
When I click a delete button, a prompt will come up. The prompt works well but somehow it is displaying a weird [object Object], [object Object]. I want it to be such that, when the prompt comes up it refers to the name
in the object. For example, when I click the delete button, the prompt will come up on browser like this,
Do you want to delete jabss ?
But now it is showing,
Do you want to delete [object Object],[object Object],[object Object],[object Object] ?
Here is the delete function button logic and where it is used in the code. How can I fix this ?
const handleDelete = (id) => {
const newPerson = persons.filter((person) => person.id !== id)
if (window.confirm(`Do you want to delete ${newPerson} ?`)) {
personService
.delete(id)
setPersons(newPerson)
}
}
It is used below in the Person
component,
<ul>
{persons.filter((person) => {
if (searchTerm === "") {
return person
} else if (person.name.toLowerCase().includes(searchTerm.toLowerCase())) {
return person
}
}).map((person, id) => {
return (
<Person key={person.id} person={person} deleteButton={()=>handleDelete(person.id)} text='Delete' />
);
})
}
</ul>
db.json where the data is stored,
{
"persons": [
{
"name": "Arto Hellas",
"number": "040-123456",
"id": 1
},
{
"name": "Ada Lovelace",
"number": "39-44-5323523",
"id": 2
},
{
"name": "jabss",
"number": "21321312321",
"id": 4
},
{
"name": "kamal",
"number": "231321321321",
"id": 5
},
{
"name": "jabri juhinin",
"number": "321321321321",
"id": 6
}
]
}
答案1
得分: 1
你需要使用 find
方法来返回单个记录,而不是使用 filter
方法,后者返回一个数组,然后获取 name
属性。
const handleDelete = (id) => {
const newPersons = persons.filter((person) => person.id !== id);
const newPerson = newPersons?.[0];
const newPersonName = newPerson?.name;
if (newPersonName && window.confirm(`Do you want to delete ${newPersonName} ?`)) {
personService.delete(id);
setPersons(newPersons);
}
}
英文:
You need to use find
method to return just single record instead of filter
method which returns array
, then get the name
property.
const handleDelete = (id) => {
const newPersons = persons.filter((person) => person.id !== id)
const newPersonName = newPersons?.[0]?.name
if (newPersonName && window.confirm(`Do you want to delete ${newPersonName} ?`)) {
personService
.delete(id)
setPersons(newPersons)
}
}
答案2
得分: 0
Step 1: 修复过滤逻辑
const newPerson = persons.filter((person) => person.id !== id)
应该改为 const newPerson = persons.filter((person) => person.id === id)
。
Step 2: 使用 find 而不是 filter,以获取一个结果而不是结果数组。
const newPerson = persons.find((person) => person.id === id)
Step 3: 在字符串中显示人的名字,而不是整个人对象。
if (window.confirm(`您是否要删除 ${newPerson.name} ?`)) {
英文:
Step 1: fix the filter logic
const newPerson = persons.filter((person) => person.id !== id)
should be const newPerson = persons.filter((person) => person.id === id)
.
Step 2: use find and not filter, to get 1 result and not an array of results.
const newPerson = persons.find((person) => person.id === id)
step 3: display the persons name, not the entire person object, in your string.
if (window.confirm(`Do you want to delete ${newPerson.name} ?`)) {
答案3
得分: 0
const handleDelete = (id) => {
const newPersons = persons.filter((person) => person.id !== id);
const personToDelete = persons.find((person) => person.id === id);
if (window.confirm(`是否要删除 ${personToDelete.name} ?`)) {
personService.delete(id);
setPersons(newPersons);
}
}
英文:
Something like this would work.
const handleDelete = (id) => {
const newPersons = persons.filter((person) => person.id !== id);
const personToDelete = persons.find((person) => person.id === id);
if (window.confirm(`Do you want to delete ${personToDelete.name} ?`)) {
personService
.delete(id)
setPersons(newPersons)
}
}
答案4
得分: 0
你可以在不需要执行单独的`.find`或额外的`.filter`的情况下,一次性保存要删除的人。
const handleDelete = (id) => {
let personToDelete;
const newPersons = persons.filter((person) => {
if (person.id === id) {
personToDelete = person;
return false; // 返回并不是必需的,但看起来更酷
}
return person.id !== id;
})
if (window.confirm(`你确定要删除 ${personToDelete.name} 吗?`)) {
personService
.delete(id)
setPersons(newPersons)
}
}
英文:
you can save the person to delete temporary all at once without doing a separate .find
or an extra .filter
const handleDelete = (id) => {
let personToDelete;
const newPersons = persons.filter((person) => {
if (person.id === id) {
personToDelete = person;
return false; // return is not necessary but looks cooler
}
return person.id !== id
})
if (window.confirm(`Do you want to delete ${personToDelete.name} ?`)) {
personService
.delete(id)
setPersons(newPersons)
}
}
答案5
得分: 0
我认为不需要再次查找已删除的人。
您可以直接将Person对象作为参数传递。
<Person key={person.id} person={person} deleteButton={() => handleDelete(person)} text='Delete' />
const handleDelete = (person) => {
const newPerson = persons.filter((person) => person.id !== id);
if (window.confirm(`Do you want to delete ${person.name} ?`)) {
personService.delete(person.id);
setPersons(newPerson);
}
}
英文:
I think. there's no need to find deletedPerson again.
You can simply pass Person object as a parameter.
<Person key={person.id} person={person} deleteButton={()=>handleDelete(person)} text='Delete' />
const handleDelete = (person) => {
const newPerson = persons.filter((person) => person.id !== id)
if (window.confirm(`Do you want to delete ${person.name} ?`)) {
personService
.delete(person.id)
setPersons(newPerson )
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论