我試圖用我的自定義函式來串聯一個物件,我只是為了練習而做。但是這個函式并沒有回傳我想要的東西。
下面是整個函式和我傳入的物件。
const data = { hello: "world", is: true, nested: { count: 5 }; {span class="hljs-attr">count: 5 } };
const stringify = (obj, symbol, numRepeat, stringifiedObj) => {
const keys = Object.keys(obj)。
const values = Object.values(obj)。
// console.log("stringified object when functino runs", stringifiedObj);/span>
// console.log("ALL KEYS", keys);.
// console.log("ALL VALUES", values);
keys.forEach((key, index) => {
// console.log(typeof obj[key]); {
if (typeof values[index] !== "object") {
console.log(values[index])。
// console.log(`{${key}: ${values[index]}}`);
stringifiedObj = `${key}: ${values[index]}.
`。
console.log("strinbgify inside if".toLocaleUpperCase(), stringifiedObj)。
} else {
console.log("this is Object"/span>, obj[key])。
stringify(obj[key], symbol, values, stringifiedObj)。
}
});
return `{${stringifiedObj}}`。
};
console. log("FUNCTION RETURN"/span>, stringify(data, "|-", 2, ""))。)
你可以忽略符號和numrepeat引數,因為我將在后面使用它們。
所以預期的結果是
hello: world
is: true true
嵌套:{
count: 5.
}
但它回傳;
hello: world
is: true。
我哪里做錯了?
uj5u.com熱心網友回復:
這應該是一個固定的版本。我在代碼中添加了一些注釋。
基本上,在遞回中你通常需要回傳一個值,這個值將在嵌套的迭代中被抓取。
Array.ForEach 不回傳任何東西,只是執行每個專案的代碼。而Array.map則回傳結果。
Array.ForEach沒有回傳任何東西,只是執行每個專案的代碼。
。const data = { hello: "world", is: true, nested: { count: 5 }; {span class="hljs-attr">count: 5 } };
const stringify = (obj, symbol, numRepeat, stringifiedObj) => {
const keys = Object.keys(obj)。
const values = Object.values(obj)。
//這里你需要RETURN一些東西,所以array.map將一個陣列映射到其他東西,你可以回傳一個值。
return keys.map((key, index) => {
if (typeof values[index] !== "object" ) {
// so return here the .map iteration.
return stringifiedObj `${key}: ${values[index]}.
`; // You really need that
在最后?
} else {
// else, return your other string {
return stringify(obj[key], symbol, values, stringifiedObj)。
}
});
//由于我們做了 -return keys.map- 下面這個就不再需要了。
//return stringifiedObj;
};
console。 log("FUNCTION RETURN"/span>, stringify(data, "|-", 2, ""))。)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可能喜歡這種簡單的型別分析技術,在t?.constructor上使用switch。每個案例只執行每個型別的 "形狀 "所需的內容,并遞回地在型別的子元素上呼叫stringify。通過添加一個默認的depth = 1引數,我們能夠使用適當的縮進行來格式化輸出=
function stringify (t, depth =1) {
switch (t?.constructor) {
case String:
return `"${t.replace(/"/g, '/span>)}"`
case Object:
return `{${linebreak(depth)}${
Object。
. entries(t)
.map(([k,v]) => `${k}: ${stringify(v, depth 1)}`)
.join(`,${linebreak(depth)}`)
}${linebreak(depth - 1)}}`
case Array:
return `[${linebreak(depth)}${
t
.map(v => stringify(v, depth 1)
.join(`,
${" .repeat(depth)}`)
}${linebreak(depth - 1)}】`
default:
return String(t)
}
}
function linebreak (depth, s = " "/span>) {
return `。
${s.repeat(depth)} `
}
const data =
{ hello: "world", is: true, nested: { count: 5, any: null, with: [ "array", undefined, { object: NaN } ] } }
console.log(stringify(data))
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
。
{
hello: "world"。
is: true。
嵌套: {
count: 5,
any: null。
with: [
"array",
undefined,
{
object: NaN
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/319300.html
標籤:
上一篇:腳本。遞回回傳未定義
