简体   繁体   English

Typescript Object.keys() 总是返回一个空数组

[英]Typescript Object.keys() always returns an empty array

First of all, I would like to mention that although this question has been asked several times before, none of the solutions seem to work for me.首先,我想提一下,虽然这个问题之前已经被问过好几次了,但似乎没有一个解决方案对我有用。

Basically, I am using Ionic React, and at one point in my code, I have an Object which I declare as:基本上,我使用的是 Ionic React,并且在我的代码中的某一时刻,我有一个 Object 声明为:

const [allArrays, setAllArrays] = useState<AllArraysObject>({});

AllArraysObject is a custom type which is declared in another file as: AllArraysObject是一种自定义类型,在另一个文件中声明为:

export interface myType{
  a: number,
  b: string,
  ...
}

export type AllArraysObject= {
  [key: string]: myType[]
}

What I actually expect to create is an object which looks like this:我实际上期望创建的是一个 object,它看起来像这样:

{
    "1": [
        {a: 10, b: "Hello", ...},
        {a: 11, b: "Hi", ...},
        ...
    ],
    ...
}

which is essentially an Object of Arrays of Objects .这本质上是 Objects 的 Arrays 的 Object The Issue is that I cannot use Object.keys(AllArrays) to get an array of all the keys of the object (like ["1", "2", ...] ).问题是我不能使用Object.keys(AllArrays)来获取 object 的所有键的数组(如["1", "2", ...] )。 I have used the same method previously in the application when it worked perfectly fine (at that point, I had an Object of objects ).我以前在应用程序中使用过相同的方法,当时它工作得非常好(那时,我有一个 Object对象)。

I have tried the following:我尝试了以下方法:

Object.keys(allArrays)
Object.keys(allArrays as Object)
Object.keys(JSON.parse(JSON.stringify(allArrays)))
Object.getOwnPropertyNames(allArrays)

each of which returns me an empty Array(0) .每个都返回一个空的Array(0) As expected, if I console.log(allArrays) , I can see all the properties of the object, and on running the above-mentioned functions in the console, I do get the desired output.正如预期的那样,如果我console.log(allArrays) ,我可以看到 object 的所有属性,并且在控制台中运行上述功能时,我确实得到了所需的 output。


I wish to know what mistake I am committing here (which could possibly be because of a gap in my understanding of how Objects work) and I also seek an explanation of the above behavior.我想知道我在这里犯了什么错误(这可能是因为我对对象如何工作的理解存在差距)并且我还寻求对上述行为的解释。 Any help would be deeply appreciated.任何帮助将不胜感激。

Remember that useState has an initial value, in this case it's an empty object {} .记住 useState 有一个初始值,在这种情况下它是一个空的 object {} I would assume you are logging the values before they are set.我假设您在设置值之前记录它们。

const Component = () => {
  const [allArrays, setAllArrays] = useState<AllArraysObject>({});

  useEffect(() => {
    const sampleObject = { "1": [ { a: 10, b: "Hello" }, { a: 11, b: "Hi" } ] }
    setAllArrays(sampleObject);
  }, [])

  console.log(Object.keys(allArrays))
  // This be Array(0) on mount until setAllArrays is set
  // when useEffect issues a side-effect

  // so make a guard if object is empty
  if (Object.keys(allArrays).length === 0 && obj.constructor === Object) {
    // null else set to anything you desire
    return null;
  }

  return (
    <div>{/* use allArrays */}</div>
  )
}

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

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