Can I generate new array with another variable in javascript? [duplicate] - javascript

This question already has answers here:
What do square brackets around an expression mean, e.g. `var x = a + [b]`?
(7 answers)
Closed 7 years ago.
function construct(head, tail) {
return cat([head], _.toArray(tail));
}
I'd like to know the role of '[' and ']' above.
Is it an operator?
Is it the array initialize literal?
This question is answered here and on the previous QnA(Use of [square brackets] around JavaScript variables).
I felt this weird because I am not used to javascript.
But this is not a matter any more.
I thought wrong and this is explained. Thanks!

It is initializing a 1 element array to pass to cat here. Brackets can be used to access elements on objects and arrays, and to represent an array literal like you have here.

Related

In JavaScript what is this method called where you might define a variable or property as such variable_name$ref [duplicate]

This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?

How to make string to object or array format [duplicate]

This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 4 years ago.
I have to create a string in format like below
"currentState[0]['children'][1]"
But I need to execute it later just like below
currentState[0]['children'][1]
I have elements and childrens on currentState. But while looping I have to create a string. But later I need to execute as array.
I have tried almost all array methods. Array.call, bind etc. And string methods as well. Could not get the output
How can I make it
Please be more specific with your question but from my understanding, you can use javascript's eval() function to execute a string as javascript, so when you need to execute it, just run eval("currentState[0]['children'][1]").
The alternative to the eval() would be
function evalFn(obj) {
return Function('"use strict";return (' + obj + ')')();
}
evalFn("currentState[0]['children'][1]")
refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval for a more in-depth explanation.

Why use '{' and '}' to define a javascript variable [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 4 years ago.
I can't seem to find anything in the documentation to understand why the { and } characters are wrapped around variables on some examples I've seen. Can anyone example why we would do that?
For example:
const {app} = require('./app/app.js');

Syntax - what does square brackets around a variable declaration mean [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

Double String Variable Name (Javascript) [duplicate]

This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Convert string to variable name in JavaScript
(11 answers)
Closed 7 years ago.
I know there are other questions similar to this, but I feel as if they don't answer my question. I would basically like to make Dynamic Variable Names. Let me give you an example:
//Take the strings "new" and "variable" and make them into a variable.
"new" + "variable" = "Hello World";
//So technically the variable name is "newvariable" and has the value "Hello World"
So basically it takes two strings, and combines them into one variable name. How would I go about doing this?
P.S. This is NOT to combine the values of variables, just the names
Write it into an array or object
var arr = {};
arr['new' + 'variable'] = 'Hello World';
then
alert(arr['newvariable']);
or
alert(arr.newvariable);

Categories

Resources