Search code examples
javascriptarraysmappingfso

How to map a list of array with selective index?


I have a list of Array with filenames and filepath. Is group into a list of array.

Example :

**var files = [filenameA, filenameB, filenameC, filepathA, filepathB, filepathC]**

I need to map them and convert them to -->

var remapfiles = {[filenameA,filepathA],[filenameB,filepathB], [filenameC,filepathC]}

The reason it was compile in files it was extracted using an SDK function where it will retrieve all filesnames and path.

How should i map/sort this to get remapfiles result and to log ? Please advice.


Solution

  • Assuming filepaths always come after filenames, you can try:

    let files = ['filenameA', 'filenameB', 'filenameC', 'filepathA', 'filepathB', 'filepathC']
    let remapfiles = [];
    for (let i = 0; i < files.length/2; i++) {
      remapfiles.push([files[i], files[files.length/2+i]])
    }
    console.log(remapfiles)