Object literals and dynamic variables [duplicate] - javascript

This question already has an answer here:
Trouble referencing variable in Collections.where method within render function
(1 answer)
Closed 9 years ago.
Can someone point me in the right direction? How do you pass a reference variable in to an object literal in JavaScript? I am using Backbone.js and specifically I am using the collections.where method. So I have something as follows:
var temp = customers.where({num: 10});
However, what if someone has a variable like var x (that changes) and they want to say something like the following:
var temp = customers.where({num: x});
JavaScript won't let you do this, I know. But how is it done or how do you get around it?

You create a closure over x like this:
var x = 10;
var filter = function() { return customers.where({num: x}); };
var temp = filter(); // uses x = 10
x = 20;
temp = filter(); // uses x = 20

Related

JavaScript: Is it possible to declare a variable using another variable's value? [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 1 year ago.
Here is an example of what I'm trying to accomplish:
const a = 'name';
const ${a} = 1;
The second variable should be:
const name = 1;
Is this possible? Thank you in advance.
Could use an object though, something like
var obj;
var x = "name";
obj[x] = 1;
console.log(obj[x]);
const a = 'name';
eval (a + " = 37");
This will create a variable name with the value 37.
However, I prefer Nobel Eugene's solution as a better approach to the problem.

Assign index (i) value at the end of variable name using a for loop (JS) [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 2 years ago.
I've been struggling with a for loop issue. I would like to declare variables using a for loop, such that with each iteration of the for loop I have a new variable with an added index number as the end.
Here's an example of what I mean
for (var i = 1; i <= 8; i++) {
ingroupProfileText+i = console.log(i);
}
So, with each iteration, the loop is effectively doing the following:
ingroupProfileText1 = console.log(1);
ingroupProfileText2 = console.log(2);
ingroupProfileText3 = console.log(3);
ingroupProfileText4 = console.log(4);
ingroupProfileText5 = console.log(5);
ingroupProfileText6 = console.log(6);
ingroupProfileText7 = console.log(7);
ingroupProfileText8 = console.log(8);
I've looked around and I keep coming across suggestions where some suggest to use an array, eval, or window. I want something locally, and I haven't been able to make it work either way.
Any help would be much appreciated :)
Why not use object**[instead of eval which is evil and hard to maintain]** and use the key as a variable.
Just a suggestion for such a dynamic things.
let indexes = {};
for (var i = 1; i <= 8; i++) {
indexes["ingroupProfileText" + i] = i;
}
const {ingroupProfileText1, ingroupProfileText2} = indexes
console.log({ingroupProfileText1, ingroupProfileText2})
console.log(indexes["ingroupProfileText5"])
That is not possible. You can't declare variables outside of that loop. You have only 3 options.
Array
Eval
Window
And you should use an array for this purpose.

declaring object using variable [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I am trying to use a variable when declaring an object:
var name1 = "object1";
var data1 = 3;
create_object(name1, data1);
function create_object(name, data) {
var x = {
name: data
}
return x
}
I want x to be stored as
var x = {
object1: 3
}
But my function will make
var x = {
name: 3
}
Is there a way to pass a variable when declaring the name of a child inside an object?
Thanks a lot
To specify a name of a property from a variable you need to use the square brackets notation like this:
function create_object(name, data) {
var x = {};
x[name] = data;
return x;
}

Javascript Function with String/Object attached to it [duplicate]

This question already has answers here:
Add method to string class
(6 answers)
Closed 4 years ago.
In javascript, I want to write a function which is called as follows:
var x = 'testString'
var y = 'anotherstring'
var z = 0
var result = x.aFunction(y, z)
function aFunction(y, z) {
...
}
This is the first time I am attempting this, my question is how can I get and use the value of x in the function aFunction, without actually referring to the declared variable.
I tried looking for this but I cannot find anything. If there is a post specifically for this that anyone knows about, please let me know.
Thanks
You need to use String.prototype.aFunction so that you can add a custom function aFunction() to the prototype of String such that it can be invoked by a string variable. Also this.toString() inside the prototype function will give you the value of the x variable (calling string)
var x = 'testString'
var y = 'anotherstring'
var z = 0
String.prototype.aFunction = function(y, z){
console.log(this.toString());
return y+z;
}
var result = x.aFunction(y, z);
console.log(result);

How can I convert a string to a variable name in Node.js? [duplicate]

This question already has answers here:
Use variable's value as variable in javascript
(2 answers)
Closed 8 years ago.
//Admin.js
var insertAdminFeed = function(s, id, timestamp){
var admin_att_new_key = '12345';
var admin_att_new_key2 = 'abc';
var admin_att_new_key3 = 'zyzyz';
var s = 'admin_att_new_key';
console.log(global[s]); //should print '12345'
};
exports.insertAdminFeed = insertAdminFeed;
I want to convert a string to a variable in node.js (I have many keys, and I don't want to write if/else statements for all of them) How can I do that?
This is not really possible in JavaScript.
You'd usually use an object literal to achieve similar needs.
var key = 'foo';
obj[key] = 1;
obj['foo'];
To be thorough, it is technically possible in JS using eval. But really, don't do this.
eval("var "+ name + " = 'some value';");
eval("console.log("+ name +")");

Categories

Resources