Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
true
You can use just forEach method of native array , in underscore its _.each
forEach
_.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}
Add a comment
var res = {}; var objs = [ { id: 50 }, { id: 61} ]; objs.forEach(function (d) { res[d.id] = true; });
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
truerepresent?