我正在嘗試映射一個物件的陣列。我不明白為什么comment.comment_comment是 "未定義"。我以為map和點符號在物件被嵌套在陣列中的情況下是有效的,我是不是理解錯了?
我最終想為第一個回圈顯示 "k"(commenter_comment: 'k'),以此類推...
import React from 'reaction'。
export default function Comments(props) {
var holder = [] 。
if (props.post && props.post.comments_text) {
holder.push(Object.values(props.post.comments_text)
return (
<div>/span>
{holder.map((comment) => {
回傳(
<div>
{comment.comment_comment}。
</div> {comment.comment_comment}</div>
)
})}
</div>>
)
} else {
return null;
}
作為參考,props看起來像這樣:
props = {
post: {
comment: true,
comments_text:
{
-Mk0Bzc8T-p6ATuwn_2T: {commenter_comment: 'k'/span>, commenter_handle: 'SHARK1', commenter_uid:
'1lJQ2rVtzRdgp4K1SZJbXAopsfm1'}。
-Mk0CdvBkgIwfsZATPsS: {commenter_comment: 's', commenter_handle: 'SHARK1', commenter_uid:
'1lJQ2rVtzRdgp4K1SZJbXAopsfm1'}。
-Mk0Cht7il7Ohn6OiziB: {commenter_comment: 's', commenter_handle: 'SHARK1', commenter_uid:
'1lJQ2rVtzRdgp4K1SZJbXAopsfm1'}。
-Mk-AnrSJVU7sOmeVP_n: {commenter_comment: 'testing14', commenter_handle: 'SHARK1',
commenter_uid: '1lJQ2rVtzRdgp4K1SZJbXAopsfm1'}。
}
handle: "bug1"。
key: "-MjLYDJlHXnO-HaL6R58"。
title: "k".
}
}
感謝您的幫助或提示。
uj5u.com熱心網友回復:
你要讓你的holder成為一個包含單個值的陣列,而這個值將是物件的值陣列:
var holder = []
if (props.post && props.post.comments_text) {
holder.push(Object.values(props.post.comments_text)
你想映射所有的物件值,而不是holder。雖然你可以訪問holder[0],但我認為如果你完全洗掉它,你的代碼就不會那么混亂了。
export default function Comments(props) {
const comments = props.post?.comments_text。
return !comments
? null !
: Object.values(comments).map(>comment => (
<div>()
{comment.commenter_comment}。
</div>/span>
));
};
uj5u.com熱心網友回復:
你可以試試這個
。import React from 'react';
import {useState}. from 'reaction;
export default function Comments(props) {
const [ holder, setHolder] = useState(Object. values(props.post.comments_text))。)
return (
holder.length != 0 ?
<div>?
{holder.map((comment) => {
回傳(
<div>
{comment.comment_comment}。
</div> {comment.comment_comment}</div>
)
})}
</div> : null
)
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/321027.html
標籤:
上一篇:不能將CSS居中
