1

I have a list of objects:

[Object { name="abc", id=12, addr="random addr 0"}, Object { name="def", id=76, addr="random addr 1"}]

What I want:

Key value pairs of name -> id

Object { abc=12, def=76}

What I have have:

for (var i in list){
    res[list[i].name]=list[i].id;
}

My question:

Is there a better way to do this?

5
  • 1
    Do you realize that this Object { abc=12, def=76} isn't valid Javascript? Do you mean { abc:12, def:76}? Commented Jun 6, 2014 at 22:06
  • Yes. Well it prints Object { abc=12, def=76} in the firebug console. Commented Jun 6, 2014 at 22:08
  • So your problem is, that you don't know how to iterate a list. Commented Jun 6, 2014 at 22:13
  • No. My question was to get a better solution (that @rplantiko provided). Commented Jun 6, 2014 at 22:25
  • 3
    This question appears to be off-topic because it is asking for code review. Commented Jun 6, 2014 at 23:52

4 Answers 4

1

For most index operations on arrays there are better alternatives (e.g. expressions with map(), reduce(), etc.). In this case, I would suggest reduce():

var s = [{ name:"abc", id:12, addr:"random addr 0"}, 
         { name:"def", id:76, addr:"random addr 1"}]; 
s.reduce( function( acc, next) { 
   acc[next.name] = next.id; 
   return acc; 
   }, {} )

gives

Object {abc: 12, def: 76}
Sign up to request clarification or add additional context in comments.

4 Comments

Why is reduce any better than other iteration methods here such as .forEach()? When you're not changing the accumulator value, I don't see how reduce offers anything over other means of iteration.
The accumulator value is changed - with each iteration, the hash grows by one new member - and this is precisely why reduce is a good approach for this problem, being of the type "Add something to the acc with each list entry, and in the end return acc as result"
You're adding a property to the variable the accumulator points to, but not changing the accumulator variable itself. I still don't see how .reduce() offers anything useful here over other forms of iterating. I find Artjom's answer much easier code to follow.
acc does not "point to a variable" but is a variable, pointing to some memory segment (technically); the content of this segment is what we call its value, and this value is changed with each iteration (as I wrote). This is a typical reduce task. Of course, every reduce can be written as a forEach instead, with the accumulator variable declared separately, like in Artjom's solution.
1

Iterate over all list items and add a property to the intended object.

var res = {};
list.forEach(function(item){
    return res[item.name] = item.id;
});

or for general array like objects

var res = {};
Arrays.prototype.forEach.call(list, function(item){
    return res[item.name] = item.id;
});

or in old school iteration

var res = {};
for(var i = 0; i < list.length; i++){
    return res[list[i].name] = list[i].id;
});

5 Comments

If you're going to use .forEach on an array, why not just list.forEach()?
@jfriend00 Because you can't be sure every time that it is an array. In this case I probably could just do list.forEach. This general approach also works for objects that are accessed like arrays.
The OP uses array syntax.
I am already iterating through a list. You can see that in the question. .foreach() might be slightly better though.
@user2815547 What better way can there be than iterating through the list. Elements won't magically rearrange themselves. Reduce looks eloquent but does it really provide something better? Is it easier to read? I don't think so.
0

Another variation, using JQuery map function ($.map)

var list = [{ 'name':"abc", 'id':12, 'addr':"random addr 0"},
            { 'name':"def", 'id':76, 'addr':"random addr 1"}];

var result = {};
$.map(list,function (i){ result[i.name] = i.id})

console.log(result);

Output:

Object {abc: 12, def: 76}

1 Comment

$.map should probably be $.each because you don't return anything.
-1

You could use jquery .map

I did as follows

var PAIDs = SU.Details.Actions.map(function (pa) { return pa.id; });

which gave me an array of objects with just the id

Comments

Your Answer

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