简体   繁体   English

如何访问 arrays 对象数组中的 object 反应?

[英]how to access object in array of arrays of objects in react?

I did this:我这样做了:

const getData = async (array,s,number) => {
            const response = await axios.get(s);
            const theData = response.data
            array[number]=theData
        }

then this:那么这个:

let array=[]
array.push(0)
getData(array, props.location.groupArray[i].link,0) //await axios.get(s) here returns an array of objects
console.log(array) //prints Array(1) 
console.log(array[0])//prints 0
console.log(array[0].length) //prints undefined

my question is why is array[0] here 0 and not an array of objects even though when I clck on Array(1) it shows:我的问题是为什么 array[0] 这里是 0 而不是对象数组,即使当我在 Array(1) 上单击时它显示:

"0: (99) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…},"

I need array to be an array of arrays of objects, in the original code I call getData more than once with 3rd parameter 0,1,2... after I push 0 to array, my problem here is I'll only know during runtime how many axios.get I'm gonna perform thats why I wanted an array of arrays of objects我需要数组成为 arrays 对象的数组,在原始代码中,我用第三个参数 0,1,2 多次调用 getData ...在我将 0 推送到数组之后,我的问题是我只会在运行时有多少 axios.get 我要执行这就是为什么我想要一个 arrays 对象数组

After you did:在你这样做之后:

array.push(0)

The content of your array became:你的数组的内容变成了:

[0]

That is why accessing the first position of the array with array[0] returns the value 0 , and printing array itself returns an Array object with length 1 ( Array(1) ).这就是为什么使用array[0]访问数组的第一个 position 返回值0 ,并且打印array本身返回一个长度为1Array object ( Array(1) )。

On the other hand, since getData() is an asynchronous function, the content of array remains unchanged when you use console.log .另一方面,由于getData()是一个异步的 function,所以使用console.logarray的内容保持不变。

So you should for example use await for it if your code is inside an async function or use .then() and .catch after the function call with console.log inside it to handle both Promise resolve or reject cases.因此,例如,如果您的代码在async function 内,或者在 function 调用之后使用 .then .then().catch并在其中使用console.log来处理 ZA5A3F0F287A4479AZFE 解决或拒绝案例,那么您应该例如使用await来处理它。

Note: top-level await, aka calling await outside an async function, is not available yet in JavaScript, it is currently just a proposal for a future version注意:顶级等待,也就是在异步 function 之外调用 await,在 JavaScript 中尚不可用,目前只是对未来版本的提议

I think using useState is good way to set array of objects to array in this problem and use useEffect for watching result我认为使用 useState 是在这个问题中将对象数组设置为数组并使用 useEffect 观察结果的好方法

const [array,setArray] = useState([0])

const getData = async (array,s,number) => {
        const response = await axios.get(s);
        const theData = response.data
        setArray(prev=>prev.push(theData)
    }

also,还,

useEffect(()=>{
getData(array, props.location.groupArray[i].link,0)
},[]);

use other useEffect for watching result使用其他 useEffect 观看结果

useEffect(()=>{
console.log(array)
},[array]);

don't forget set off StrictMode in index.js不要忘记在 index.js 中设置 StrictMode

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

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