Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am stuck with this problem. I have a function to accept the path and also the same time I have a variable that I want to condition with.
Here is my problem: I want to make a string type that will act as an access to my variable.
In my situation, I have a roles.operation variable which I want to access it dynamically.
The roles variable has an array with the values of:
roles.operations = ['document','article','document-type'];
with this variable I want this to be access dynamically.
Here is what I've tried, which in replacePath i have the value of document-type:
export const createVariable = (roles,path) => {
const replacePath = path.replace(/-/g,"_");
const finalPath = window[`roles.operations.${replacePath}`];
console.log(finalPath);
}
this gives me undefined.
Try this way:
const finalPath = window['roles']['operations'][replacePath];
or
const finalPath = window.roles.operations[replacePath];
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Below is the line of code from a file, and I want to replace the currentVersion value with something else.
I have parsed the file and converted to string. only need a way to replace version.
private currentVersion = "1.26.1.3";
Expected output
private currentVersion = "<different version value>";
Note:
Above line occurs once in file.
version value could be anything
How can it be done with regex and replace in Javascript?
You could replace by captured group
const code = `
import React from "react";
export default App() {
private currentVersion = "1.2.3.4";
}
`
const replaced = "5.6.7.8"
const res = code.replace(
/(private currentVersion = ")(.*)(")/gm,
`$1${replaced}$3`
)
console.log(res)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I try to createElement as h1 and its attribute class, but i can not get it working.
here goes my post:
function krea_ta(){
var tagy = document.cteateElement("h1");
var clasy = createAttribute("class");
clasy.value = "myclass";
tagy.setAttributeNode(clasy);
tagy.innerText = "business";
}
Where is the problem?
First things first, you've spelled "create" wrong on "createElement".
Secondly, you can either set the class as an attribute using tagy.setAttribute("class", "myclass")
or via the classList property which expects an array of strings..
tagy.classList = ["myclass"];
Edit:
The above assign of the array might not be working on all browsers (works on Chrome), as MDN states that classList is a read-only property.
tagy.classList.add(["myclass"]);
The .add method accepts parameterised values, array of values and a single value.
Why not simply do it like this:
function krea_ta(){
const tagy = document.createElement("h1");
const tagyText = document.createTextNode("business");
tagyText.appendChild(tagyText);
tagy.classList.add("myClass");
document.body.appendChild(tagy);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to create an object inside an object in JavaScript like this:
{
"test": {
{},
{}
}
}
In more detail, I want to create a new object if "test" doesn't exist in the object, and create a new object in the same location as the old one if "test" exists.
Please help if this is possible.
You mean, like:
theObject["test"] = {};
this?
It's just an assignment, mate!
You need to give a key in every object you create in JavaScript.
E.g.:
var obj = {
objInside: {}
}
you can read more about object literals.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi this is a simple question. I was wondering is there any different when you declare something like this. Thanks
selectedData[key](val)
and
selectedData[key] = val
This line selectedData[key](val) is not a declaration, it's calling the function that is stored under the key key in the object selectedData and it's passing the parameter val to that function.
The other line selectedData[key] = val is assigning the value val to the key key in the object selectedData.
In the first case, you're calling whatever is in selectedData[key] as a function with val as an argument while in the second one you're assigning it.