I have the following object:

```
[{animal: 1, hasLegs: true},{animal: 1, hasTail: false},
 {animal: 2, hasLegs: true},{animal: 2, hasTail: true},{animal: 3}]

```

I would like to restructure it to the following:

```
[
  { animal:1, info: { hasLegs: true, hasTail: false },
  { animal:2, info: { hasLegs: true, hasTail: true },
  { animal:3, into: {},
]

```

such that all objects in the array with that is `animal:1` is unified under that key and adding the remaining keys with values under the key `info`.

Been trying to use the `map()`-function but can not get the hang of it, and do not know if `map()` alone can solve this.
['https://Stackoverflow.com/questions/58511850', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2762175/']
I would suggest creating a lookup object from the initial array. This way, you don't need to look through the entire array on every element (which would make it `O(n²)`).

So given our list beforehand:

```js
const animals = [
  { animal: 1, hasLegs: true },
  { animal: 1, hasTail: false },
  { animal: 2, hasLegs: true },
  { animal: 2, hasTail: true },
  { animal: 3 }
]

```

We create a lookup table with `reduce`, merging every object that we find in the original array into the value object of the animal:

```js
const lookup = animals.reduce((acc, val) => {
  // use object destructuring and object spread to pick out
  // the "animal" field and the rest into separate variables
  const { animal, ...info } = val
  // get existing info object, or create new empty if not found
  const existingInfo = acc[animal] || {}
  // merge existing info and new info from this object
  acc[animal] = { ...existingInfo, ...info }
  return acc
}, {})

```

At this point, the structure is like this:

```js
{
  '1': { hasLegs: true, hasTail: false },
  '2': { hasLegs: true, hasTail: true },
  '3': {}
}

```

Now we just need to go through this object and turn it into an array, and pick the key into the "animal" field:

```js
const after = Object.entries(lookup).map(([key, value]) => {
  return { animal: key, info: value }
})

```

This produces the result we want:

```js
[
  { animal: '1', info: { hasLegs: true, hasTail: false } },
  { animal: '2', info: { hasLegs: true, hasTail: true } },
  { animal: '3', info: {} }
]

```

Note though that `Object.entries` may not be supported in older browsers. If you're targeting older browsers, check out how Robb Traister does it in his answer with `Object.keys`
```js
const array = [{ animal: 1, hasLegs: true }, { animal: 1, hasTail: false },
{ animal: 2, hasLegs: true }, { animal: 2, hasTail: true }, { animal: 3 }];
const result = [];
array.forEach(obj => {
    let newEntry = null;
    if (result.findIndex(entry => entry.animal === obj.animal) === -1) {
        newEntry = { animal: obj.animal, info: {} }
        result.push(newEntry);
    }
    const entry = newEntry || result.find(entry => entry.animal === obj.animal);
    Object.keys(obj).forEach(key => {
        if (key === 'animal') return;
        entry.info[key] = obj[key];
    });

});
console.log(result);

```