Question: I have an array that looks like this:

```
const arr = [{
    name: 'One',
    id: 1
  },
  {
    name: 'Two',
    id: 2
  }
];

```

I then want to return just the name of the object if the id matches.

I have tried something like the following, but it returns the whole object in an array:

```js
const arr = [{
    name: 'One',
    id: 1
  },
  {
    name: 'Two',
    id: 2
  }
];

const getNameFromId = id => {
  return arr.filter(item => {
    if (item.id === id) {
      return item.name;
    }
  })
}

const res = getNameFromId(1)
// This should return `'One'`
console.log(res)
```

Answer: 
You may prefer to use `find` instead.

```
const id = 2
arr.find(val => val.id === id).name

```

you should check if `find` return a value or not

```
const getNameFromId = id => {
  const search = arr.find(val => val.id === id)
  if (search) {
    return search.name
  }
}

```
If you use `filter` (which returns an array) you can grab the `name` from the first element. `find` as mentioned in the other answer might better suit your needs, however.

```js
const arr = [{ name: 'One', id: 1 }, { name: 'Two', id: 2} ];

const getNameFromId = (id) => {
  return arr.filter(item => item.id === id)[0].name;
}

const res = getNameFromId(1)
console.log(res)
```