如何将道具的道具从一个位置转移到另一个位置? [英] How to transfer props of an object from one location to another?
本文介绍了如何将道具的道具从一个位置转移到另一个位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是具有某些深层属性的对象.
Following is an object with some deep properties.
const InitialObj = {
Store: {propsFromStore: {}},
Market: {propsFromMarket: {}},
GoDown: {propsFromDown: {}},
fruits: {store_c: "Store", otherProp1: {store_e: "Store", otherSubProp1: {}}, otherProp2: {}},
vegetables: {market_d: "Market", otherProp1: {otherSubProp1: {godown_r: "GoDown", }}},
fancy:{store_t: "Store", market_d: "Market", otherProp1: {otherSubProp1: {godown_r: "GoDown", }}},
}
无论您在何处看到商店",市场"或"GoDown",都应将其替换为以下对象中的值-无论它们的深度如何.
Where ever you see "Store", "Market" or "GoDown", it should get replaced with values from the following object - no matter how deep they are.
// these values are not separate. there are part of the above initialObj.
//These values should replace Where ever you see "Store", "Market" or "GoDown", no matter how deep they are.
// The following values should be obtained from the above initialObj. & Not as a separate object.
Store: {propsFromStore: {}},
Market: {propsFromMarket: {}},
GoDown: {propsFromDown: {}},
这样最终结果将是:预期结果
So that the final result will be: expected result
// this is what we get when the initialObj is transformed
// note that after removing all the occurences of "Store"/ "Market" / "GoDown"; these values will be removed so that final result looks like this. (with all placeholders replaced"
const finalExpectedResult = {
fruits: {store_c: {propsFromStore: {}, otherProps...}, otherProp1: {store_e: {propsFromStore: {}, otherProps...}, otherSubProp1: {}}, otherProp2: {}},
vegetables: {market_d: {propsFromMarket: {}, otherProps...}, otherProp1: {otherSubProp1: {godown_r: {propsFromDown: {}, otherProps...}, }}},
fancy:{store_t: {propsFromStore: {}, otherProps...}, market_d: {propsFromMarket: {}, otherProps...}, otherProp1: {otherSubProp1: {godown_r: {propsFromDown: {}, otherProps...}, }}},
}
请注意,这应将商店",市场"或下乡"的所有深度事件替换为相应的值
推荐答案
您可以使用第一个函数来查找哪些道具应作为参考(商店,市场,GoDown),并在其他道具上使用递归,并带有辅助功能功能:
You can use a first function which will find which props should serve as references (Store, Market, GoDown), and use recursion on the others, with an auxiliary function:
const InitialObj = {
Store: {propsFromStore: {}},
Market: {propsFromMarket: {}},
GoDown: {propsFromDown: {}},
fruits: {store_c: "Store", otherProp1: {store_e: "Store", otherSubProp1: {}}, otherProp2: {}},
vegetables: {market_d: "Market", otherProp1: {otherSubProp1: {godown_r: "GoDown", }}},
fancy:{store_t: "Store", market_d: "Market", otherProp1: {otherSubProp1: {godown_r: "GoDown", }}},
};
function hydrateObj(obj, keys) {
const refs = keys.reduce((o, k) => ({...o, [k]: obj[k]}), {});
return Object.entries(obj).reduce((res, [k, v]) => {
if(!keys.includes(k)) {
res[k] = hydrateObjAux(v, keys, refs);
}
return res;
}, {});
}
function hydrateObjAux(obj, keys, refs) {
return Object.entries(obj).reduce((res, [k, v]) => {
if(keys.includes(v)) {
res[k] = refs[v];
} else {
res[k] = hydrateObjAux(v, keys, refs);
}
return res;
}, {});
}
const res = hydrateObj(InitialObj, ['Store', 'Market', 'GoDown']);
document.body.innerHTML = '<pre>' + JSON.stringify(res, 0, 4) + '</pre>';
这篇关于如何将道具的道具从一个位置转移到另一个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文