I have a string lists/listA/listB/listC/...
.
Please tell me how I can split the string to this view and write the result to an array:
[ {0: lists}, {1: lists/listA}, {2: lists/listA/listB}, {3: lists/listA/listB/listC}, {4: lists/listA/listB/listC/...}, {...: .../...} ]
Advertisement
Answer
Here is the solution:
let splitedArray = 'lists/listA/listB/listC/...'.split('/'); let string = ''; const result = splitedArray.map((item, index) => { return {[index]: string = string + (!index ? item : `/${item}`)}; }); console.log('result', result)