簡體   English   中英

JS替換對象中的占位符值

[英]JS Replace Placeholder value in object

我有模板,其中的占位符指定如下。 .event.id.event.properties.file ,...)

瞧,我想用下面的event -object的值替換模板中的占位符。

 var templates = [ { a:"1", a2: {some:"other Obj"}, a3: ["or","array"], b: ".event.id", b2: "'.event.name' and more strings", }, { dontKnowKeys: "of where the .event will be in", mightNotBeHere: "no placeholder here", butHereAgain: ".event" andSomeDeep: ".event.properties.file is Full" } ]; var event = { id: "1234-uuid-something", name: "I Am an awesome Event", properties: { file: "/root/foo", type: "error" } }; // First thought (pretty dirty, huh?) JSON.parse(JSON.stringify(templates).replace(someMagicRegex, event)) // Second thought templates = templates.map((tpl) => { return _.values(tpl).replace('.event.([az]+)',event["$1"]); }; 

但這不適用於properties.file鍵。

此外,如果層次結構被任意地.event.properties.file.cwd.path ,該怎么.event.properties.file.cwd.path

畢竟,我希望擁有以下對象:

 var out = [ { a:"1", a2: {some:"other Obj"}, a3: ["or","array"], b: "1234-uuid-something", b2: "'I Am an awesome Event' and more strings", }, { dontKnowKeys: "of where the {\\"id\\":\\"1234-uuid-something\\",\\"name\\":\\"I Am an awesome Event\\",\\"properties\\":{\\"file\\":\\"/root/foot\\",\\"type\\":\\"error\\"}} will be in", mightNotBeHere: "no placeholder here", butHereAgain: "{\\"id\\":\\"1234-uuid-something\\",\\"name\\":\\"I Am an awesome Event\\",\\"properties\\":{\\"file\\":\\"/root/foot\\",\\"type\\":\\"error\\"}}" andSomeDeep: "/root/foo is Full" } ]; 

為您嘗試了一個解決方案。 試用

注意:這僅適用於具有簡單數組和對象的當前模板結構。 正則表達式在選擇鍵后需要空間。 根據您的需要進行更改。

 var templates = [ { a: "1", a2: {some: "other Obj"}, a3: ["or", "array"], b: ".event.id", b2: "' .event.name ' and more strings", }, { dontKnowKeys: "of where the .event will be in", mightNotBeHere: "no placeholder here", butHereAgain: ".event.properties.type.depth.key", andSomeDeep: ".event.properties.file is Full" } ]; var event = { id: "1234-uuid-something", name: "I Am an awesome Event", properties: { file: "/root/foo", type: {depth:{key:"i am deep"}} } }; // First thought (pretty dirty, huh?) //JSON.parse(JSON.stringify(templates).replace(someMagicRegex, event)) // Second thought templates = templates.map((tpl) => { return handleObject(tpl); }); function handleObject(obj) { for (let keyname in obj) { obj[keyname] = checkTypes(obj[keyname]) } return obj; } // assume you have objects and arrays also function checkTypes(value) { if (typeof value === 'string') { return replaceVal(value); } else if (_.isArray(value)) { //assume array has only strings return value.map((d)=>replaceVal(d)) } else if (_.isObject(value)) { return handleObject(value); } } function replaceVal(str) { //console.log(str); return str.replace(/.event.([^\\s]+)/, (match,capture)=>_.get(event,capture)) } console.log(templates); 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM