Skip to content
Advertisement

Split paths and hashes into 2 different variables

I have one variable. The variable looks like this:

path1/path2,hash
path1/path2,hash
path1/path2,hash
path1/path2,hash

etc, etc.

How do I get 2 different variables, one containing the paths and the other containing the hashes?

For example, the paths will be like this:

path/path
path/path
path/path
path/path

I’m new to javascript btw, sorry.

Advertisement

Answer

If I understand you correctly, you want to split all the content into two multi line string, then this peace of code could help to you.

let arrOfPathAndHash = `path1/path2,hash
path1/path2,hash
path1/path2,hash
path1/path2,hash`.split('n');

arrOfPathAndHash = arrOfPathAndHash.map((pathAndHash) => pathAndHash.split(','));

const paths = arrOfPathAndHash.map((pathAndHash) => pathAndHash[0]).join('n');
// path1/path2
// path1/path2
// path1/path2
// path1/path2

const hashes = arrOfPathAndHash.map((pathAndHash) => pathAndHash[1]).join('n');
// hash
// hash
// hash
// hash
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement