seperate comma seperated string values in string variable - javascript

I am trying to extract the string which is like
var str = "[\"/home/dev/servers\", \"e334ffssfds245fsdff2f\"]"
Desired ouput
a = "/home/dev/servers"
b = "e334ffssfds245fsdff2f"

Here you are:
const str = "[\"/home/dev/servers\", \"e334ffssfds245fsdff2f\"]";
const object = JSON.parse(str);
const a = object[0];
const b = object[1];
console.log(a);
console.log(b);

The following will work fine for you.
var str = "[\"/home/dev/servers\", \"e334ffssfds245fsdff2f\"]";
var foo = JSON.parse(str); //Parse the JSON into an object.
var a = foo[0];
var b = foo[1];

Using JSON.parse()
let [a, b] = JSON.parse("[\"/home/dev/servers\", \"e334ffssfds245fsdff2f\"]")
console.log(a)
console.log(b)

Related

Combine array in JavaScript with underscore character

Var a = "837297,870895"
Var b = "37297,36664"
OutPut = "837297_37297,870895_36664
I tried multiple script but does not working
You can split and map the splitted array as such:
var a = "837297,870895"
var b = "37297,36664"
var output = a.split(',').map((e,i)=> e+"_"+b.split(',')[i])
console.log(output.toString())
For ES2015 or lower
var a = "837297,870895"
var b = "37297,36664"
var output = a.split(',').map(function(e,i){return e+"_"+b.split(',')[i]})
console.log(output.toString())
You can start by converting the two strings (lists) into arrays using .map() and .split() methods.
Then use .map() and .join() methods to produce the desired string (list):
const a = "837297,870895";
const b = "37297,36664";
const [c,d] = [a,b].map(e => e.split(','));
const output = c.map((e,i) => `${e}_${d[i]}`).join();
console.log( output );

Initialise an array with same value, x times

If I have:
let a = [1,3,4,5];
how do I dynamically set b to have the same length as a with each entry containing "<", i.e.
Expected result:
b = ["<","<","<","<"];
You can use Array#map:
const a = [1,3,4,5];
const b = a.map(() => "<");
console.log(b);
You can use Array#from:
const a = [1,3,4,5];
const b = Array.from(a, () => "<");
console.log(b);
Or you can use Array#fill:
const a = [1,3,4,5];
const b = new Array(a.length).fill("<");
console.log(b);
Here's a solution:
Array(a.length).fill('<');

Javascript string to special array

I have a string:
var string = "test,test2";
That I turn into an array:
var array = string.split(",");
Then I wrap that array into a larger array:
var paragraphs = [array];
Which outputs:
[['test','test2']]
But I need it to output:
[['test'],['test2']]
Any clue how I can do this?
Just map each item into an array containing that item:
var string = "test,test2";
var result = string.split(",").map(x => [x]);
// ^--------------
console.log(result);
let test_string = "test,test2";
let result = test_string.split(',').map(item => [item])
console.log(result)
You can get expected result using array Concatenation.
var string = "test,test2";
var array = string.split(",");
var finalArray=[[array[0]]].concat([[array[1]]])
console.log(JSON.stringify(finalArray));
What version of JavaScript are you targeting?
This might be a general answer:
var arr = "test,test2".split(",");
var i = arr.length;
while(i--)
arr[i] = [arr[i]];

Regex for creating an array from String

I have a string as follows
var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";
I want to get three arrays from above string as follows
var arr1 = ["series-3","series-5","series-6"];
var arr2 = ["a3","a4","a5"];
var arr3 = ["class a", "class b"];
What regex should I use to achieve this?
Can this be done without regex?
Use String#split() method
var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";
// split string based on comma followed by [
var temp = str.split(/,(?=\[)/);
// remove [ and ] from string usning slice
// then split using , to get the result array
var arr1 = temp[0].slice(1, -1).split(',');
var arr2 = temp[1].slice(1, -1).split(',');
var arr3 = temp[2].slice(1, -1).split(',');
console.log(arr1, arr2, arr3);
Or same method with some variation
var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";
// Remove [ at start and ] at end using slice
// and then split string based on `],[`
var temp = str.slice(1, -1).split('],[');
// then split using , to get the result array
var arr1 = temp[0].split(',');
var arr2 = temp[1].split(',');
var arr3 = temp[2].split(',');
console.log(arr1, arr2, arr3);
RegEx and String methods can be used. It's better to create an object and store individual arrays inside that object.
var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";
// Match anything that is inside the `[` and `]`
var stringsArr = str.match(/\[[^[\]]*\]/g);
// Result object
var result = {};
// Iterate over strings inside `[` and `]` and split by the `,`
stringsArr.forEach(function(str, i) {
result['array' + (i + 1)] = str.substr(1, str.length - 2).split(',');
});
console.log(result);
var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";
var stringsArr = str.match(/\[[^[\]]*\]/g);
var result = {};
stringsArr.forEach(function(str, i) {
result['array' + (i + 1)] = str.substr(1, str.length - 2).split(',');
});
console.log(result);
To create the global variables(Not recommended), just remove var result = {}; and replace result by window in the forEach.
I would prefer to do it like this
var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]",
arrs = str.match(/[^[]+(?=])/g).map(s => s.split(","));
console.log(arrs);
Just for the fun of it, another way where we add the missing quotes and use JSON.parse to convert it to a multidimensional array.
var str = "[series-3,series-5,series-6],[a3,a4,a5],[class a,class b]";
var result = JSON.parse("[" + str.replace(/\[/g,'["').replace(/\]/g,'"]').replace(/([^\]]),/g,'$1","') + "]");
console.log(result[0]);
console.log(result[1]);
console.log(result[2]);

How to merge String?

It is possible to merge two String with Angularjs function ?:
"123456"
"ABC"
to :
"ABC456"
Thankx
try
var str1 = "123456";
var str2 = "ABC";
console.log(str2 + str1.substring(str2.length));
Use String.prototype.substr function:
var a = "123456";
var b = "ABC";
var res = b + a.substr(b.length);
document.write(res);

Categories