以下代碼中的錯誤是什么?我嘗試使用地圖迭代物件陣列。但我收到一個錯誤。請找到附件代碼以供參考。
import "./styles.css";
export default function App() {
const sample = [{
samplePath : "/website",
par:{
abc:"123",
def: "678",
ghi:"456"}
}];
const createURL = (re)=> {
const q = Object.entries(re.par)
.map(([key, value]) => key '=' value).join("&");
return "?" q;
}
console.log(createURL(sample));
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
我收到以下錯誤

uj5u.com熱心網友回復:
你想叫Object.entries上re.par。在這種情況下,re是一個陣列,因此呼叫.par它回傳 undefined。我不確定您想要的結果是什么,但是如果您訪問陣列中的第一個元素,然后.par,也許這就是您想要的?
const sample = [{
samplePath : "/website",
par:{
abc:"123",
def: "678",
ghi:"456"}
}];
const createURL = (re)=> {
const q = Object.entries(re.par)
.map(([key, value]) => key '=' value).join("&");
return "?" q;
}
console.log(createURL(sample[0]));
uj5u.com熱心網友回復:
在您的示例中sample是一個陣列,其中包含一個物件。
因此,要么洗掉 [],這樣 sample 就是一個物件,就像您在代碼中參考它一樣。或者,Object.entries(re[0].par)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/338063.html
標籤:javascript 反应
