简体   繁体   English

访问对象的属性时如何在对象名称中使用数字变量

[英]How can I use a numeric variable in object name when access the object's properties

 var showcaseIndex = 1; var showcaseItem1 = { projectTitle: "Adrian's Caterers", projectRole: "Web Designer & Developer <br />", rojectClient: "Catering Business Owner", projectDescription: "" }; var showcaseItem2 = { projectTitle: "Inspiring", projectRole: "Web Designer & Developer", projectClient: "Medical Trasnport Start-up", projectDescription: "" }; $('.showcase-content h1').html(showcaseItem[showcaseIndex].projectTitle); $('.showcase-content #role').html(showcaseItem[showcaseIndex].projectRole); 
 .showcase-content { width: 500px; height: 70vh; background: rgba(255,255,255,1); float: left; box-sizing: border-box; padding: 20px; } .showcase-content h1, h2, p { color: #4e4e4e; text-align: left; margin: 0; font-family: 'quicksandregular', sans-serif; margin-bottom: 10px; } 
 <div class="showcase-content"> <h1></h1> <h2 id="role">Role in Project <br /> Client</h2> <h2>Description</h2> <p> Some Content inserted with PHP</p> </div> 

Hi guys, kinda off my coding game here so I need some help. 大家好,我的编码游戏还不错,所以我需要一些帮助。 I'm building my portfolio website and the page in which will display my past projects will be full screen. 我正在建立我的投资组合网站,将显示我过去的项目的页面将变为全屏。 I've stored information in objects with the same name with the exception of the number at the end. 我将信息存储在同名对象中,但末尾的数字除外。 There is also an variable index, my goal was change the content of my portfolio page by clicking on an arrow which will start a function to change the content, but I can't seem to get pass making the content show up period. 还有一个变量索引,我的目标是通过单击箭头来更改我的投资组合页面的内容,这将启动一个功能来更改内容,但是我似乎无法通过使该内容显示期间。 I hope I'm making sense here, if not I will be happy to clarify further. 我希望我在这里很有道理,如果没有的话,我很乐意进一步澄清。

How can I use a variable(numeric) as apart of an object name to access that objects properties. 如何使用变量(数字)作为对象名称的一部分来访问该对象的属性。

According to the Zach-M and ASDFGerte suggestions, use an Array instead an object and get your profile settings with an index. 根据Zach-M和ASDFGerte的建议,使用Array代替对象,并使用索引获取配置文件设置。

In this solution, every property is written in the doc, not just two. 在此解决方案中,每个属性都写在文档中,而不仅仅是两个。

Notice the correspondence between the ID properties in the HTML tag and the property names in the profiles. 注意HTML标记中的ID属性与配置文件中的属性名称之间的对应关系。 It's for readability and convenience. 这是为了便于阅读和方便。

 (function () { "use strict"; $(document).ready(function () { var showCaseIndex = 1; // so the second one let showcase = [ { projectTitle: "Adrian's Caterers" , projectRole: "Web Designer & Developer" , projectClient: "Catering Business Owner" , projectDescription: "" }, { projectTitle: "Inspiring" , projectRole: "Web Designer & Developer" , projectClient: "Medical Transport Start-up" // <- typo here "trasnport" , projectDescription: "" } ] for(var prop in showcase[showCaseIndex]) { $(".showcase-content #"+prop).html(showcase[showCaseIndex][prop]); } }); }()); 
  .showcase-content { width: 500px; height: 70vh; background: rgba(255,255,255,1); float: left; box-sizing: border-box; padding: 20px; } .showcase-content h1, h2, p { color: #4e4e4e; text-align: left; margin: 0; font-family: 'quicksandregular', sans-serif; margin-bottom: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showcase-content"> <h1 id="projectTitle"></h1> <h2 id="projectRole">Role in Project <br /> Client</h2> <p id="projectClient"></p> <h2 id="projectDescription">Description</h2> <p id="projectDescription"></p> <p> Some Content inserted with PHP</p> </div> 

You could use eval to evaluate your object's name and access it that way, but doing so is seriously NOT recommended . 您可以使用eval评估对象的名称并以这种方式访问​​它,但绝对不建议这样做。

As mentioned by Zach M. and ASDFGerte , using an Array is definitely the way to go here. 正如Zach M.ASDFGerte所述 ,使用Array绝对是解决问题的方法。

Functional example: 功能示例:

 (function () { "use strict"; $(document).ready(function () { var showcase = []; var showcaseIndex = 1; showcase.push({ projectTitle: "Adrian's Caterers", projectRole: "Web Designer & Developer <br />", // <-- <br /> here seems out of place... rojectClient: "Catering Business Owner", projectDescription: "" }); showcase.push({ projectTitle: "Inspiring", projectRole: "Web Designer & Developer", projectClient: "Medical Trasnport Start-up", projectDescription: "" }); $('.showcase-content h1').html(showcase[showcaseIndex].projectTitle); $('.showcase-content #role').html(showcase[showcaseIndex].projectRole); }); }()); 
 .showcase-content { width: 500px; height: 70vh; background: rgba(255,255,255,1); float: left; box-sizing: border-box; padding: 20px; } .showcase-content h1, h2, p { color: #4e4e4e; text-align: left; margin: 0; font-family: 'quicksandregular', sans-serif; margin-bottom: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showcase-content"> <h1></h1> <h2 id="role">Role in Project <br /> Client</h2> <h2>Description</h2> <p> Some Content inserted with PHP</p> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何才能将变量用作对象名称(子对象的大小写) - how i can to use variable into object name (sub object case) 从对象数组的元素中,如何访问对象的属性/方法。 JS - From an Object's Array's Element, how can I access the Object's properties/methods. Js 如何访问对象的属性 - how can i access properties of an object 如何在动态对象变量的名称中使用下划线 - How can I use underscore in name of dynamic object variable 如何使用变量或函数名称作为JavaScript中对象属性的占位符? - How to use a variable or a function name to be a place holder for a object's properties in JavaScript? 如何将变量设置为对象的键,以便可以使用javascript动态访问对象? - How do I set a variable to be a object's key so I can access an object dynamically in javascript? 对象属性名称中的变量 - Variable in the object properties name 如何增加 object 的内容及其属性和属性值? - How can I increment content of object and it's properties and properties value? 如何从对象访问变量? - How can I access variable from object? 如何访问黄瓜“步骤”对象的 json 的所有“文本”属性并将它们记录到控制台? - How can I access all the “text” properties of the json of cucumber's “step”-object and log them to the console?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM