12

I've read this answer on SO to try and understand where I'm going wrong, but not quite getting there.

I have this function :

get() {
    var result = {};

    this.filters.forEach(filter => result[filter.name] = filter.value);

    return result;
}

It turns this :

[
    { name: "Some", value: "20160608" }
]

To this :

{ Some: "20160608" }

And I thought, that is exactly what reduce is for, I have an array, and I want one single value at the end of it.

So I thought this :

this.filters.reduce((result, filter) => {
    result[filter.name] = filter.value;
    return result;
});

But that doesn't produce the correct result.

1) Can I use Reduce here?

2) Why does it not produce the correct result.

From my understanding, the first iteration the result would be an empty object of some description, but it is the array itself.

So how would you go about redefining that on the first iteration - these thoughts provoke the feeling that it isn't right in this situation!

5
  • 1
    This is not a filter job. If you want to receive a dictionary object it must be done by reduce. Try @Pranav C Balan's answer. Commented Jun 8, 2016 at 15:20
  • 1
    I'm not using a filter, or stated filter at all.... Commented Jun 8, 2016 at 15:26
  • @Redu They aren't using filter. They have an array named filters but they're performing a reduce on that array. Commented Jun 8, 2016 at 15:50
  • @Mike C i meant the first snippet in the question. Commented Jun 8, 2016 at 15:54
  • @Redu I know. Read the code. They aren't using filter. It's a forEach which is being used like reduce. Commented Jun 8, 2016 at 15:58

2 Answers 2

37

Set initial value as object

this.filters = this.filters.reduce((result, filter) => {
    result[filter.name] = filter.value;
    return result;
},{});
//-^----------- here

var filters = [{
  name: "Some",
  value: "20160608"
}];

filters = filters.reduce((result, filter) => {
  result[filter.name] = filter.value;
  return result;
}, {});

console.log(filters);

var filters = [{
  name: "Some",
  value: "20160608"
}];

filters = filters.reduce((result, {name, value}= filter) => (result[name] = value, result), {});

console.log(filters);

Sign up to request clarification or add additional context in comments.

4 Comments

You could also use parameter destructuring to shorten this a bit: (result, {name, value} = filter) => (result[name] = value, result)
That doesn't give me the output I'm expecting... could you do it in plnkr or something?
@CallumLinington : check the snippets added
Wait one minute, I didn't see that last parameter you supplied to the reduce function.. And I completely missed that when reading the MDN docs. I might as well, delete the question ....
2

Since 2019 (ES2019) you can go with Object.fromEntries() but you need to map to array first.

const filtersObject = Object.fromEntries(filters.map(({ name, value }) => [name, value])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.