1

I have an array looking like this:

[Object { id=50, name="My Town"}, Object { id=61, name="My second town"}]

which I need to transform to:

Object { 61=true, 50=true}

How do I do that using underscore.js (or regular js)?

thanks Thomas

1
  • What does that true represent? Commented Nov 24, 2013 at 10:41

2 Answers 2

2

You can use just forEach method of native array , in underscore its _.each

 var arr = [{ id:50, name:"My Town"},{ id:61, name:"My second town"}];
 var obj = {};
 arr.forEach(function(val){   obj[val.id] = true; });
 console.log(obj)//Object { 61=true, 50=true}
Sign up to request clarification or add additional context in comments.

Comments

1
var res = {};
var objs = [ { id: 50 }, { id: 61} ];
objs.forEach(function (d) { res[d.id] = true; });

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.