在React JS中显示结果为 [object Object], [object Object]

huangapple go评论73阅读模式
英文:

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) =&gt; {
    const newPerson = persons.filter((person) =&gt; 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,

    &lt;ul&gt;
          {persons.filter((person) =&gt; {
            if (searchTerm === &quot;&quot;) {
            return person
          } else if (person.name.toLowerCase().includes(searchTerm.toLowerCase())) {
            return person
          }
          }).map((person, id) =&gt; {
            return (
            &lt;Person key={person.id} person={person}  deleteButton={()=&gt;handleDelete(person.id)} text=&#39;Delete&#39; /&gt;
          );
          })
          }
   &lt;/ul&gt;

db.json where the data is stored,

{
  &quot;persons&quot;: [
    {
      &quot;name&quot;: &quot;Arto Hellas&quot;,
      &quot;number&quot;: &quot;040-123456&quot;,
      &quot;id&quot;: 1
    },
    {
      &quot;name&quot;: &quot;Ada Lovelace&quot;,
      &quot;number&quot;: &quot;39-44-5323523&quot;,
      &quot;id&quot;: 2
    },
    {
      &quot;name&quot;: &quot;jabss&quot;,
      &quot;number&quot;: &quot;21321312321&quot;,
      &quot;id&quot;: 4
    },
    {
      &quot;name&quot;: &quot;kamal&quot;,
      &quot;number&quot;: &quot;231321321321&quot;,
      &quot;id&quot;: 5
    },
    {
      &quot;name&quot;: &quot;jabri juhinin&quot;,
      &quot;number&quot;: &quot;321321321321&quot;,
      &quot;id&quot;: 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) =&gt; {
    const newPersons = persons.filter((person) =&gt; person.id !== id)
    const newPersonName = newPersons?.[0]?.name
    if (newPersonName &amp;&amp; window.confirm(`Do you want to delete ${newPersonName} ?`)) {
      personService
        .delete(id)
        setPersons(newPersons)
    }
  }

答案2

得分: 0

Step 1: 修复过滤逻辑

const newPerson = persons.filter((person) =&gt; person.id !== id) 应该改为 const newPerson = persons.filter((person) =&gt; person.id === id)

Step 2: 使用 find 而不是 filter,以获取一个结果而不是结果数组。

const newPerson = persons.find((person) =&gt; person.id === id)

Step 3: 在字符串中显示人的名字,而不是整个人对象。

if (window.confirm(`您是否要删除 ${newPerson.name} ?`)) {
英文:

Step 1: fix the filter logic

const newPerson = persons.filter((person) =&gt; person.id !== id) should be const newPerson = persons.filter((person) =&gt; 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) =&gt; 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) =&gt; {
const newPersons = persons.filter((person) =&gt; person.id !== id);
const personToDelete = persons.find((person) =&gt; 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) =&gt; {
    let personToDelete;
    const newPersons = persons.filter((person) =&gt; {
     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.

&lt;Person key={person.id} person={person}  deleteButton={()=&gt;handleDelete(person)} text=&#39;Delete&#39; /&gt;

const handleDelete = (person) =&gt; {
const newPerson = persons.filter((person) =&gt; person.id !== id)
if (window.confirm(`Do you want to delete ${person.name} ?`)) {
  personService
    .delete(person.id)
    setPersons(newPerson )
}

}

huangapple
  • 本文由 发表于 2023年2月23日 20:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定