我一直在研究如何從一個模式中生成一個陣列。我幾乎已經做到了,我正在將字串推送到陣列中,但是當它迭代到下一個欄位時,它又再次初始化了陣列,我不確定如何解決這個問題。
我包含了一個存在該問題的沙盒。 https://codesandbox.io/s/rough-architecture-i94ph?file=/src/App.js
我想要的輸出應該是[contractorName, agencyName]/code>
目前,它輸出的是
log1 [contractorName]
log2 [agencyName]
log3 []
如果有任何幫助,我將會告訴你。
如果有任何幫助,我們將不勝感激
。uj5u.com熱心網友回復:
試試這個
export const generateWatchedFields = (schema)=> {
const watchingArray = [];
function iterates(schema){
Object.values(schema).map((propertySchema) => /span>{
if (propertySchema.properties) {
iterates(propertySchema.properties)。
} else if (propertySchema.dependencies) {
const push = propertySchema.dependencies.watching。
watchingArray.push(push)。
} else {
console.log(watchingArray)。
}
});
console.log(watchingArray)。
}
iterates(schema)
return watchingArray;
};
我不完全確定這是否是你要找的東西,但它回傳["contractorName", "agencyName"]
uj5u.com熱心網友回復:
看起來問題是,你在每次呼叫generateWatchedFields時,都在generateWatchedFields中宣告你的watchingArray。
export const generateWatchedFields = (schema)=> {
const watchingArray = []; //New array on every call.
Object.values(schema).map((propertySchema) => /span>{
if (propertySchema.properties) {
return generateWatchedFields(propertySchema.properties) 。
} else if (propertySchema.dependencies) {
const push = propertySchema.dependencies.watching。
return watchingArray.push(push)。
} else {
console.log(watchingArray)。
}
});
console.log(watchingArray)。
return watchingArray;
};
因此,你只需要在這個背景關系之外宣告它,就像這樣:
var watchingArray = [] 。
export const generateWatchedFields = (schema)=> {
Object.values(schema).map((propertySchema) =>; {
if (propertySchema.properties) {
return generateWatchedFields(propertySchema.properties) 。
} else if (propertySchema.dependencies) {
const push = propertySchema.dependencies.watching。
return watchingArray.push(push)。
} else {
console.log(watchingArray)。
}
});
console.log(watchingArray)。
return watchingArray;
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/311639.html
標籤:
上一篇:給出兩個串列,單詞和定義。我們需要創建一個叫做cooldictionary的字典,我們用單詞作為鍵,用定義作為值。
