Question: I've been struggling with defining a function that returns the permutations of a given array.
I have written the code below:

```
def gPermutations(array):
    result = []
    idx = 0
    for element in array:
        for i in range(len(array)):
            if i != idx:
                z = array[:array.index(element)]
                z.append(array[i])
                s = z[:] + array[array.index(element)+1:]
                s[i] = element
                result.append(s)
                idx += 1
    return result

```

This code seems to work by returning some of the permutations of an array, but doesn't return all of them, and sometimes duplicates a certain permutation. May someone please explain what is the issue with my code? Thanks!

Answer: 
The solution depends on the structure of `filesList`. If it contains some simple js array, it would look quite similiar to the following.

```js
const data = [{
    filesList: {
      size: 2,
      _origin: 0,
      _capacity: 2,
      _level: 5,
      _root: null,
      array: ["abc", "cde"]
    },
    id: "089b5559-171f-46b0-9aa4-1c9fc5013b0b",
    name: "Arabidopsis TAIR10",
    value: "Arabidopsis",
  },
  {
    filesList: {
      size: 2,
      _origin: 0,
      _capacity: 2,
      _level: 5,
      _root: null,
      array: ["ghi", "jkl"]
    },
    id: "da8f7da3-04e3-4aba-98e3-615e86982a60",
    name: "Arabidopsis TAIR10",
    value: "Arabidopsis",
  },
  {
    filesList: {
      size: 2,
      _origin: 0,
      _capacity: 2,
      _level: 5,
      _root: null,
      array: ["qwe", "ert"]
    },
    id: "6b166a90-f73b-4000-b18c-8f7b80513e6d",
    name: "Bean GCF_000499845.1",
    value: "Bean_reference_genome",
  }
]

const result = data.reduce((acc, entry) => {
  acc[entry.value] = (acc[entry.value] || []).concat(entry.filesList.array);
  return acc;
}, {});

console.log(result);
```
Here is a quick way of doing it. I'm assuming that you want an object where the key is the `name` and the value is the `fileList`.

```
var betterStructure = {};
for(var i = 0; i < array.length; i++){
    let {name, filesList} = array[i];
    if(betterStructure.hasOwnProperty(name)){
        betterStructure[name] = betterStructure[name].concat(filesList);
    }
    else{
        betterStructure[name] = filesList;
    }
};

```

Edit: Made the logic that handles duplicates.