What is altarnative of eval for strings in javascript [duplicate] - javascript

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
hi what can I use inspite of eval function for that code :
product_pcs = 12;
product_code = "lkb_12" ;
eval(product_code +"_pcs_1 = product_pcs");
alert (lkb_12_pcs_1);

I recommend you build an object instead
const products = {};
prodcuts['lkb_12'].pcs = 12;
prodcuts['lkb_12'].name = 'test';
prodcuts['lkb_13'].pcs = 14;
prodcuts['lkb_13'].name = 'test2';
// ...
``

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.

Link to array push function [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
Basically why I can't do this:
var a = [];
var b = a.push;
b(1);
it give's error, I know how to avoid this, but why this is not working in js?
The this value is determined by how the function is called. You can use Function#bind to ensure the this value is set.
var a = [];
var b = a.push.bind(a);
b(1);
console.log(a);
Try this:
var a = [];
var b = (param) => a.push(param);
b(1);

I want to get size of Javascript object [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 2 years ago.
I created an object in JavaScript and stored some information in the object. I searched a lot for solution, but did not find any solution. I want to get the count/length of object.
Javascript Code:
productDetail = new Object();
productDetail["Code"] = "A104";
productDetail["ProductName"] = "Keyboard";
productDetail["UnitPrice"] = 10;
productDetail["Dealer"] = "AMD";
This is what you are looking for:
Object.keys(productDetail).length
You are after the count of the property keys of the object.
const productDetail = new Object();
productDetail["Code"] = "A104";
productDetail["ProductName"] = "Keyboard";
productDetail["UnitPrice"] = 10;
productDetail["Dealer"] = "AMD";
console.log(Object.keys(productDetail).length)

Node js add dynamic property [duplicate]

This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 5 years ago.
let fileName = "test.c";
let testCase = "Case1";
let test = {};
test.fileName = testCase;
console.log(test)
I need fileName property to be dynamic
What is need is, like below
{
"test.c":"Case1"
}
Can any one help me
test.fileName = testCase;
Won't work in this case. Should be
test[fileName] = testCase;
You can use the ES6 computed property syntax:
{
[fileName]: "Case1"
}
This will be interpreted dynamically as:
{
"test.c": "Case1"
}

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