Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

javascript - 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", }}},
}

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...}, }}},
}

Note that this should replace all the deep occurences of "Store", "Market" or "GoDown" with corresponding values

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The obvious solution would be to simply recursively iterate over all object properties, however it might be unnecessarily complicated and time consuming for such a simple case. Assuming we want to replace all instances of values being strings like "Store" and so on, we could transform the object into a string and do a simple replace function on it's contents, turning it back into object afterwards.

const obj = {
    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" }}}
}


// please note the existence of ":" in front of the searched term which ensures the replaced value is actually a value not a key of the nested objects
const replaced = JSON.parse(JSON.stringify(obj)
  .replace(/:"Store"/g, ": {"propsFromStore": {}}")
  .replace(/:"Market"/g, ": {"propsFromMarket": {}}")
  .replace(/:"GoDown"/g, ": {"propsFromDown": {}}"))

console.log(replaced)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...