You can use reduce for this, the accumulator should be an object containing two values, the first will be the results array and the other will be the current hash string to be used within reduce to generate the paths. Once reduce finishes its work we can store the result property of the accumulator into our result variable B:
const B = A.reduce((acc, str) => { // for each string in the array A
if(str.indexOf("#") === 0) { // if the string is hashed one
acc.currentHash = str.slice(2); // set the current hash to this string
} else { // otherwise
acc.result.push(acc.currentHash + "/" + str); // use the current hash to generate the resulting string of the current string
}
return acc;
}, { result: [], currentHash: ""}).result; // once reduce finishes, use '.result' to access the result array of the accumulator
Example:
const A = [ '# test', 'test', '# layouts', 'main', 'nav' ];
const B = A.reduce((acc, str) => {
if(str.indexOf("#") === 0) {
acc.currentHash = str.slice(2);
} else {
acc.result.push(acc.currentHash + "/" + str);
}
return acc;
}, { result: [], currentHash: ""}).result;
console.log(B);
constfor something you want to change.