Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 遍历对象的嵌套数组,并用动态值替换空属性-Javascript(Loop through Nested array of Object and replace empty attribute with dynamic values - Javascript)

I have a nested object like below.(我有一个如下的嵌套对象。)

The content attribute in some of the objects are empty.(一些对象中的content属性为空。) I need to loop through all the objects ,if it has an empty content attribute i need to give a value like Default value to it.(我需要遍历所有对象,如果它具有空的content属性,则需要给它一个像Default value这样的 。) var data = [{ title: "Admin Services", content: "admin", links: [{ title: "Report", content: "", links: [{ title: "Notifications", content: "Notify", links: [{ title: "Send", content: "", links: [{ title: "read", content: "", links: [], }] }] }] }, { title: "Script", content: "script", links: [{ "title": "Execute", content: "", links: [] }] }, { title: "Process", content: "", links: [] }] }]; function checkEmptyContent(data){ data.forEach((item)=>{ if(item.links.length > 0){ checkEmptyContent(item.links) } if(item.content === ""){ item.content = "Default Content"; } }); } checkEmptyContent(data); console.log('data',data); And i got the output like below(我得到的输出如下) [ { "title": "Admin Services", "content": "admin", "links": [ { "title": "Report", "content": "Default Content", "links": [ { "title": "Notifications", "content": "Notify", "links": [ { "title": "Send", "content": "Default Content", "links": [ { "title": "read", "content": "Default Content", "links": [] } ] } ] } ] }, { "title": "Script", "content": "script", "links": [ { "title": "Execute", "content": "Default Content", "links": [] } ] }, { "title": "Process", "content": "Default Content", "links": [] } ] } ] The final requirement is ,not only add the default text but also a counter which is increments like below(最终要求是,不仅要添加默认文本,还要添加一个计数器,该计数器的增量如下所示) [ { "title": "Admin Services", "content": "admin", "links": [ { "title": "Report", "content": "Default Content (0)", "links": [ { "title": "Notifications", "content": "Notify", "links": [ { "title": "Send", "content": "Default Content (1)", "links": [ { "title": "read", "content": "Default Content (2)", "links": [] } ] } ] } ] }, { "title": "Script", "content": "script", "links": [ { "title": "Execute", "content": "Default Content (0)", "links": [] } ] }, { "title": "Process", "content": "Default Content (0)", "links": [] } ] } ] The counter should start with 0 for each of the objects in the array(对于数组中的每个对象,计数器应以0开头)   ask by codegeek translate from so

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

1 Answer

0 votes
by (71.8m points)

You can use recursion for this, by repeating the addDefault() function for each nested array.(您可以为此使用递归,方法是为每个嵌套数组重复addDefault()函数。)

By looping over each object in the array, you can check whether or not it has content , if it doesn't, you can add your "Default Content (0)" string to it:(通过遍历数组中的每个对象,可以检查它是否具有content ,如果没有,则可以向其中添加“默认内容(0)”字符串:) const data = [{title:"Admin Services",content:"admin",links:[{title:"Report",content:"",links:[{title:"Notifications",content:"Notify",links:[{title:"Send",content:"",links:[{title:"read",content:"",links:[]}]}]}]},{title:"Script",content:"script",links:[{title:"Execute",content:"",links:[]}]},{title:"Process",content:"",links:[]}]}]; const addDefault = (arr, level=0) => { arr.forEach(obj => { obj.content = obj.content || `Default Content (${level++})`; addDefault(obj.links, level); level = 0; }); } addDefault(data); console.log(data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...