This question already has answers here:
Template literal inside of the RegEx
(2 answers)
How do you use a variable in a regular expression?
(27 answers)
Closed last month.
in Typescript, I need to expand a variable inside a regular expression to build a filter for a MongoDB query; none of the following solutions seem to be valid; only the variable "code" is expanded, not the "subCode"; suppose we have:
let code = "code1";
let subCode = "CODE2.";
const regexExpression = {code: { $in: [`${code}`, /^`${subCode}`\..*/i] }};
const newRegexExpression = new RegExp(/code: { $in: [`${code}`, /^`${subCode}`\..*/i] }/i
);
Desired result:
{ "code" : { $in : [ "code1", /^"CODE2\.".*/i ] } }
Related
This question already has answers here:
Object destructuring with property names that are not valid variable names
(2 answers)
Closed 3 years ago.
The following works fine:
const o = {one:1,two:2,three:3};
const {one,...others}=o;//one=1, others={two:2,three:3}
But how would I do the following:
var o = {['this-is-one']:1,two:2,three:3};
var {['this-is-one'],...others}=o;
Currently that gives me a SyntaxError: Unexpected token ','
I suspect it would not work because this-is-one would be invalid for a constant name (it only works for property values).
You need a renaming of the variable name, because the the given key is not a valid variable name.
var o = { 'this-is-one': 1, two: 2, three: 3 },
{ 'this-is-one': thisIsOne, ...others } = o;
console.log(thisIsOne);
console.log(others);
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 3 years ago.
I am trying to create a program that defines the variable finalResult based on variable input. The input variable should call on an object inside of object A:
var input = "";
var A = {
AA: {
result: 0
},
AB: {
result: 1
}
}
var finalResult = A.input.result;
So if input = "AA", then the final result should be 0, but if input = "AB", then the final result should be 1.
You can do A[input].result, which assumes the value of input is present as a property in A. If the property isn’t present you’ll get an error trying to access result on undefined. You can guard against this by OR’ing it with an empty object:
(A[input] || {}).result // undefined if A[input] isn’t present
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
JavaScript set object key by variable
(8 answers)
Closed 5 years ago.
How can I use a variable as the name parameter in an object?
var field = 'profile.email'
var value = 'example#email.com'
var query = { field : value };
console.log(query);
Current output:
{ field : 'example#email.com' }
Output I want to achieve:
{ 'profile.email' : 'example#email.com' }
You can e.g. drop the field variable inside square brackets.
var field = 'profile.email',
value = 'example#email.com',
query = { [field] : value };
console.log(query);
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I was trying to "put a function into an object" so I wanted to do something like this but I'm getting errors everywhere.
var someobject = {
makename(1) : null,
makename(2) : null,
makename(3) : null,
makename(4) : null
};
function makename(num) {
return (identifier + ' Bot' + num)
}
In modern (ES2015) JavaScript environments, you can do this:
var someobject = {
[makename(1)]: "foo",
[makename(2)]: "bar"
};
The [ ] wrapper around the property name allows it to be an arbitrary expression. The result of evaluating the expression is interpreted as a string and used as the property name.
var someobject = {}
someObject[makename(1)] = null;
someObject[makename(2)] = null;
someObject[makename(3)] = null;
someObject[makename(4)] = null;
This works everywhere. However, #pointy's solution is nicer!
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 years ago.
Silly example:
<script>
var a = {
'b' : {
'c' : "success!!"
}
};
var d = 'b.c';
</script>
How could I access success!! if I can't go for the obvious solution a.b.c or a['b']['c'], but instead have to use d? I tried a[d], which doesn't seem to do the trick. I also tried to fiddle with eval(). Is this even possible?
If it's really necessary to have the keys in a string separated with a dot, I would use split and reduce:
var success = d.split(".").reduce(function (obj, key) {
return obj[key];
}, a);
Try splitting
var a = {
'b' : {
'c' : "success!!"
}
};
var d = 'b.c';
var splat = d.split('.');
console.log(a[splat[0]][splat[1]]);