Question: I have been struggling to get this pass from last couple of days. But could not figure out how to solve this. I'm working on a react project. Below is my problem

```js
var array = [
    {name: 'a', value: false}, 
    {name: 'b', value: false}, 
    {name: 'c', value: false}, 
    {name: 'd', value: true}, 
    {name: 'e', value: false}
]
```

I tried Javascript `filter` and `forEach` methods but couldn't figure out the perfect solution for get an array with the `names` of the objects whose `value` is `true`. Expected output for previous sample of data should be:

```
var output = ['d']

```

Answer: 
You can do it with [reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) iterating only once on the array:

```js
var array = [
    {name: 'a', value: false},
    {name: 'b', value: false},
    {name: 'c', value: false},
    {name: 'd', value: true},
    {name: 'e', value: false}
]

let res = array.reduce((acc, curr) =>
{
    curr.value && acc.push(curr.name);
    return acc;
}, []);

console.log(res);
```

Or using [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) and `destructuring`:

```js
var array = [
    {name: 'a', value: false},
    {name: 'b', value: false},
    {name: 'c', value: false},
    {name: 'd', value: true},
    {name: 'e', value: false}
]

let res = array.reduce((acc, {name, value}) => value ? [...acc, name] : acc, []);

console.log(res);
```
You could use only [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) to achieve this. You add to the acc array only when the value is true.

```js
const  array = [{name: 'a', value: false}, {name: 'b', value: false}, {name: 'c', value: false}, {name: 'd', value: true}, {name: 'e', value: false}];

const res = array.reduce((acc, { name, value }) => value ? acc.concat(name) : acc, []);

console.log(res);
```