简体   繁体   English

React:我如何遍历一个对象,即一个列表?

[英]React: How do I loop through an object, that is a list?

How to loop through an object in react?如何在反应中循环对象?

I know this is a silly question, but I cant for the life of me find the answer online.我知道这是一个愚蠢的问题,但我一生都无法在网上找到答案。

I want to loop through an object that is a list.我想遍历一个列表对象。

const [tagList, setTagList] = useState([])

Before looping, I also do this with the list:在循环之前,我也对列表执行此操作:

setTagList(makeListOfTags(tagString))

function makeListOfTags(tagLine) {
        tagLine = JSON.stringify(tagLine)
        tagLine = tagLine.replace('{', '')
        tagLine = tagLine.replace('}', '')
        tagLine = tagLine.replace('"tagString":', '')
        tagLine = tagLine.replace('"', '')
        tagLine = tagLine.replace('"', '')
        const tags = tagLine.split(" ")

        return tags;
    }

tagString is formated like this: {tagString: "tag a", "tag b"} the tags variable, is a list containing the tags in tagsString: ["tag a", "tag b"] I use setTagList to make tagList like const tags. tagString 的格式如下: {tagString: "tag a", "tag b"} tags 变量,是一个包含 tagsString: ["tag a", "tag b"] 中的标签的列表 我使用 setTagList 来制作 tagList 像常量标签。

But Im struggling to loop the tagList.但我正在努力循环 tagList。 I've tried:我试过了:

for (let i = 0; i > tagList.length; i++)

But that gives: Uncaught TypeError: Cannot read properties of undefined (reading 'lenght')但这给出了: Uncaught TypeError: Cannot read properties of undefined (reading 'lenght')

I've tried:我试过了:

for (let i = 0; i > Object.keys(tagList).length; i++)

But that gives: Uncaught TypeError: Cannot read properties of undefined (reading 'lenght')但这给出了: Uncaught TypeError: Cannot read properties of undefined (reading 'lenght')

I've tried:我试过了:

for (let i = 0; i > tagList.tagList.length; i++) {

But that gives: Uncaught TypeError: Cannot read properties of undefined (reading 'lenght')但这给出了: Uncaught TypeError: Cannot read properties of undefined (reading 'lenght')

if I do this right before the loop:如果我在循环之前这样做:

console.log(typeOf tagList)

It says that tagList is an object.它说 tagList 是一个对象。

So how do I do the loop?那么我该如何做循环呢?

You may have length spelled incorrectly in your code.您的代码中的长度可能拼写错误。

If your typeError states reading 'lenght' , check if there is a line number next to your typeError, and navigate to that line in your code.如果您的 typeError 状态reading 'lenght' ,请检查 typeError 旁边是否有行号,然后导航到代码中的该行。 There may a typo there.那里可能有错字。

Otherwise, your for loop examples use a greater than sign, rather than a less than sign.否则,您的 for 循环示例使用大于号,而不是小于号。 It should look like this:它应该如下所示:

for (let i = 0; i < Object.keys(tagList).length; i++)

If it isn't looping correctly after your typeError is fixed, your issue may be there.如果在您的 typeError 修复后它没有正确循环,您的问题可能就在那里。 If your tagList is truly an object, Object.keys should work for looping.如果您的 tagList 确实是一个对象,那么 Object.keys 应该可以用于循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM