in Education by
I'm going to be getting an array of objects and want to set instance variables inside of a class based on a property. So if I get this: ary = [{type: 'walrus', name: 'GorbyPuff'}, {type: 'humanoid', occupation: 'KingSlayer'}] I want to initialize an object where @walrus == ary[0] and @humanoid == ary[1] In Ruby I could user instance_variable_set, but how can this be accomplished in the Javascripts? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I'm not sure if I get what you're trying to acchieve, but the easiest way to do this would be: var theObj = {}; for(var i=0;i<ary.length;i++) { theObj[ary[i].type] = ary[i]; } The worry here is, that by altering the ary variable you will inadvertently alter the theObj: console.log(theObj.walrus.name);//Outputs: GorbyPuff ary[0].name = 'Nips!'; console.log(theObj.walrus.name);//Outputs: Nips! <-- objects are passed by reference, always If the ary variable is part of a function scope, and the resulting object is its return value, you needn't worry. But if both are part of the global scope (Which they shouldn't, it's bad practice), this becomes an issue. I therefore propose this approach: var obj = {}; var i; while (ary.length !== 0) { i = ary.splice(0,1)[0];//removes element from array if (i.hasOwnProperty('type'))//always best to check the property you're going to use is there { obj[i.type] = i; } }

Related questions

0 votes
    In which process, a local variable has the same name as one of the instance variables? a) Serialization ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    I am trying to return multiple variable as an output from js Script in AutoamtionAnywhere. For example I have js script ... ()]; var month_number = d.getMonth(); if(month_number...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am trying to return multiple variable as an output from js Script in AutoamtionAnywhere. For example I have js script ... ()]; var month_number = d.getMonth(); if(month_number...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Multiple variables can be created and initialized in a single JavaScript statement 1. False 2. True...
asked Feb 23, 2021 by JackTerrance
+1 vote
    Multiple variables can be created and initialized in a single JavaScript statement a)False b)True...
asked Oct 8, 2020 in Technology by JackTerrance
0 votes
    I need SSH access to an Amazon EC2 instance running Ubuntu. All I have is the Amazon username and password. Any ideas? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I currently have a UIView, call it (A), that is outsourced out into a 3rd party library. Pressing ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have inherited a large legacy ColdFusion app. There are hundreds of some sql here #variable# statements ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I got some code here, which creates a label just fine, but when I free the label it STILL shows ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    I got some code here, which creates a label just fine, but when I free the label it STILL shows ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    In my application, I'm displaying a Power BI report. It already works, so there's no problem with showing ... dataset in the report? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I found the following piece of code from the adw launcher: Object service = launcher.getSystemService("statusbar ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    Which of the following function programmatically extract parts of XML file? (a) XmlSApply (b) XmlApply (c) ... and answers pdf, Data Science interview questions for beginners...
asked Oct 29, 2021 in Education by JackTerrance
...