I have the following text: "configuration/entities/details/attributes/FirstName".
I want to break it in such a way that, attributes and FirstName should come out separately, preferably as two variables.
How can I achieve this using JavaScript?
I have the following text: "configuration/entities/details/attributes/FirstName".
I want to break it in such a way that, attributes and FirstName should come out separately, preferably as two variables.
How can I achieve this using JavaScript?
If the structure is always the same, with attribute and name at the end, you can do:
let text = "configuration/entities/details/attributes/FirstName"
let arr = text.split("/"); //["configuration", "entities","details", "attributes", "FirstName"]
let reverse_arr = arr.reverse();
let [name,attr] = reverse_arr;
console.log(name, attr)
The split
separates by "/", the reverse()
well reverses it, then with destructuring, you say that "take the first two and put it in two variables"
It wont matter if there are more or less elements elements if those two are at the end
you can do such as this:
let targetString="configuration/entities/details/attributes/FirstName";
let splitArray=targetString.split('/');
let attributes=splitArray[splitArray.length-1]