Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组(0),但内容不为空。这是什么意思?_Javascript_Data Structures_Async Await_Es6 Promise - Fatal编程技术网

Javascript 数组(0),但内容不为空。这是什么意思?

Javascript 数组(0),但内容不为空。这是什么意思?,javascript,data-structures,async-await,es6-promise,Javascript,Data Structures,Async Await,Es6 Promise,我有一段代码如下: getLog(这在一个单独的文件中) 此函数是异步的,它返回一个承诺,该承诺返回各种异步调用的结果 getItem(反应组件) res:打印数组(0),但当我在DevTools中展开它时,它会显示一个对象数组: 0: {name: "x", property:"y", otherProperty:"z"} 1: {name: "x", property:"y", ot

我有一段代码如下:

getLog(这在一个单独的文件中)

此函数是异步的,它返回一个承诺,该承诺返回各种异步调用的结果

getItem(反应组件)

  • res
    :打印数组(0),但当我在DevTools中展开它时,它会显示一个对象数组:

      0: {name: "x", property:"y", otherProperty:"z"}
      1: {name: "x", property:"y", otherProperty:"z"}
    
  • finalArr
    :打印数组(0),展开后它实际上是一个空数组

在我看来,这是一个类似数组的对象(不可编辑)

如何从该对象提取数据?

有趣的是,在React DevTools中,我可以看到数组以正确的状态存储,但当我在模板中使用
{items.length}
时,它会打印0


(如果不使用模拟数据重建async/await流,我无法生成一个最小的示例。)

@CertainPerformance这是一个很好的观点。但是,我不需要使用promise的返回值(因为我基本上是使用getItem通过hooks设置react state)我的错误,看起来问题出在
getLog
上。你能发布
getLog
的完整代码吗?解决方案可能是使用
Promise。所有
getLog
听起来都在做一些事情,而不仅仅是简单的
waiting
在它的顶层是
res1
res2
res4
getLog
中真正未声明的(全局)变量?也许是别的东西改变了他们。
const [items, setItems]=useState([])

 getItem = async () => {
    getLog().then((res: any) => {
        let finalArr: any = []
        res.map((val: any) => finalArr.push(val))
        console.log(res, finalArr)

        setItems(finalArr) 
    })
}

getItem()
  0: {name: "x", property:"y", otherProperty:"z"}
  1: {name: "x", property:"y", otherProperty:"z"}