This question already has answers here:
Get state value by a dynamic key in react
(2 answers)
Closed 5 years ago.
I already know how to setState with a dynamic key name but how do I set a value as the state with a dynamic key?
Example
function thing(key) {
let stuff = this.state.key;
//Do stuff
}
Didn't even think of think of this as first but all I did was
function thing(key) {
let stuff = this.state[key];
//Do stuff
}
and it worked!
Related
This question already has answers here:
What's the difference between passing a value and passing a callback in a React setState hook function?
(2 answers)
Closed 4 months ago.
I'm not an expert in React Js and want to know the difference between these two syntaxes in the ES6 syntax.
const items = array.map(thing => <p key={thing}>{thing}</p>)
function addItem(){
setArray(previtems => { return [...previtems, `Thing ${array.length + 1}`]});
}
and
const items = array.map(thing => <p key={thing}>{thing}</p>)
function addItem(){
setArray([...items, `Thing ${array.length + 1}`]);
}
The first one is functional setState. It gives guarantee that the value of prevItems will be the most updated one. This pattern is to be used whenever next state depends on the previous state.
This question already has answers here:
How to toggle a boolean?
(9 answers)
Closed 1 year ago.
Given boolean value completed = false how do I flip it back and forth? I could of course do something like this:
if (completed) {
completed = false
} else {
completed = true
}
But that feels hacky and too long. Is there any slick and cleaner way of doing this? Thanks.
Assuming you declared variable with let or var, so that you can reassign the value :
completed = !completed
This question already has answers here:
Is it good practice to set variables to null when they are no longer needed?
(4 answers)
Closed 6 years ago.
I was wondering if there is a need for cleaning my objects following usage in JavaScript or there is some GarbageCollector that does that for me?
for example if this is my code:
function foo()
{
var test = new testClass();
console.log(test.getMessage());
//the question is if there a need for the next line or not:
test = null;
}
var testClass = function() {
this.getMessage = function () {return "My MSG" };
}
In JavaScript there is no need not need to care about garbage collecting process since it will be handled for you.
This question already has answers here:
Union of Array of Objects in JavaScript and es6?
(9 answers)
Closed 8 years ago.
How to append a smaller list of object in another main list after checking only those elements from the smaller list are getting appended which were not previously in main list. I have to do it in javascript.
Would something like the below work?
_.each($testArray, function ($item1){
var flag = false;
_.each($setArray, function ($item2){
if($item1 == $item2)
flag = true;
});
if(flag == false)
$setArray.push(item1);
});
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to update a value in my JSON.
My JSON result looks like this:
results = {"ROWCOUNT":50,"COLUMNS":["PERSONID","NAME"],"DATA":{"PERSONID":["42","43","44"], "NAME":["JOE","TOM","JANE"]}
resultData = results.DATA
In the below code I am looping over the result set and attempting to update a value at a position. I believe it is failing because I am not using dynamic variables correctly.
var columnName = "NAME";
for(i=0; i < results.ROWCOUNT; i++ ){
resultData.columnName[i] = "foo" // failing here due to "columnName" being dynamic.
}
Figured it out.. You have to use array syntax
resultData[columName][i]