Correct way to "delete" a property? [duplicate] - javascript

This question already has answers here:
How can I unset a JavaScript variable?
(12 answers)
Closed 9 years ago.
With an Array I know you can use the delete keyword to remove items out of the Array. However, delete will not delete a variable.
var b = "some stuff";
delete b;
console.log(b); // "some stuff"
What's the correct way to "free" the memory used by b ? Does doing just b = null; do the trick?

Possible duplicate of this question about unsetting variables in Javascript with some excellent answers. Short answer - null is probably fine.

In Javascript, you cannot really 'free' any memory yourself; all you can do is remove all references to the memory that an object uses, and the JS engine's garbage collector will recover it. Setting the variable / property to null would be a good starting point.
As regards the use of 'delete', I haven't found a better resource for understanding it than http://perfectionkills.com/understanding-delete/.

Related

How to add item to object JS [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 2 years ago.
I have looked all over google and i have found nothing that fits my needs. How would you add an item to an object. I basically have a string variable that i want as the key. Therefore i cant use obj.key = 'something';. I sort of want it like:
obj = {'somekey': 'somevalue'};
obj.add('someotherkey': 'someothervalue');
console.log(obj);
by obj.add() i mean someone's solution
and then console says {'somekey': 'somevalue', 'someotherkey': 'someothervalue'}
Does anyone know a way to do this. I don't care at what position it is just whether it is there. By the way i'm using node if that helps. If you have any questions on my code please ask.
Very simple:
obj["somekey"] = "somevalue";
You can also use a variable instead:
let myVariable = "somekey";
obj[myVariable] = "somevalue";
Or you just use normal object notation:
obj.somekey = "somevalue"

How to check if the target variable is in an array? [duplicate]

This question already has answers here:
How do I check if a variable is an array in JavaScript?
(24 answers)
Closed 5 years ago.
I'm working on a chatbot in Javascript, and I have a variable called "target", which is the variable that will contain what the user says in chat, and an array called "doingGood":
let doingGood = ["good", "great", "amazing", "awesome"];
What I want to do, is I want to make an if statement that will do something if the target variable is equal to a string that's in the doingGood array. For example, if target is equal to good, great, amazing, or awesome, do something. I've looked around on the internet, but I'm stumped and can't seem to find an answer. Help would be appreciated, thanks!
EDIT: The question is the same, but I tried the methods that they suggested there and they didn't seem to work. When I tried if (variable.target === doingGood), it gave me an error saying "variable is not defined."
you can use indexOf methd of array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
if(doingGood.indexOf(target) !== -1){
// do something
}

How can I dynamically render a variable name in dot notation? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I am not sure how to accurately describe this issue, but the code should suffice. This code is in React.js, but I think it is a JavaScript question more than anything. I have a variable initialization:
const key = this.state.key;
This will evaluate to one of three strings...title, author, or year.
How can I use this variable in the following block?
let filteredBooks = books.filter(
(book) => {
return book.key.toString().toLowerCase().indexOf(this.state.query) !== -1;
}
);
Book has properties title, author, and year... that I want to query, and I would rather not switch through the possible values of key(only 3 right now), in case I want to scale with more properties for the book object later. How can I dynamically reference key, and render it on the fly to either title, author, year, or any other property that key might be. (key is not a property of book). I have tried book.eval(key), but this throws an "undefined is not a function" compilation error at eval(key). Once again, I am using React.js but I am thinking this is a general Javascript question. Thanks!
book[key] is the correct answer posted by Answer Li and Keith in the comments!

Javascript How do I force a string + variable to be evaluated as a variable [duplicate]

This question already has answers here:
is there a way to execute a function when I have its name in a string [duplicate]
(2 answers)
Closed 9 years ago.
Im not even sure how to word this and is probably why I am having trouble finding an answer in google.
When the code is run currentCardRow will equal 1 therefore it should be cardSelected1 which is what is shown in the console.log. I need it to go a step further because cardSelected1 is a variable and I need it to evaluate show in the console log as Invitation. Invitation is an example of a variable for cardSelected1.
I am not sure on what the correct syntax is to make this happen.
var currentCardSelected = "cardSelected" + currentCardRow;
Thanks for your help!
JavaScript has an Eval() function which allows you to evaluate strings as javascript code.
For example
var bar = "123";
var foo = "bar";
console.log(eval(foo));
will print "123" to the console.
For more information on eval, you can consult the MDN docs.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
Generally, the use of eval() is considered poor practice as it makes the code difficult to read. There are likely more elegant solutions to implement what you have described, however, eval will solve your current problem.
var currentCardSelected = eval("cardSelected" + currentCardRow);
This is how my problem is fixed.

Is JavaScript's array.clear() not a function? [duplicate]

This question already has answers here:
How do I empty an array in JavaScript?
(17 answers)
Closed 8 years ago.
I'm trying to empty an array containing my drawn coordinates when a button "clear" is pressed.
When I call drawnDivs.clear(), I get an error that it is not a function. drawnDivs is certainly an array, and I have Firebug console.logs printing things out. It's hosted here.
Nope, it's not. But drawnDivs.length = 0 should work.
drawnDivs = [];
It was answered in Stack Overflow question How do I empty an array in JavaScript?.
Two examples from the answer:
var A = ['some', 'values', 'here'];
//Method 1
//(This was my original answer to the question)
A = [];
// Method 2 (as suggested by Matthew Crumley)
A.length = 0
And here is a nice write up on these two methods by Dr. Axel Rauschmayer.
An optimized way to do it is:
while (arr.pop()) {}
See http://jsperf.com/kbk-clear-array/2.
You could alternately use the Prototype library and then, use Prototype's clear() method.

Categories