Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform .join on value in array of objects

If I have an array of strings, I can use the .join() method to get a single string, with each element separated by commas, like so:

["Joe", "Kevin", "Peter"].join(", ") // => "Joe, Kevin, Peter" 

I have an array of objects, and I’d like to perform a similar operation on a value held within it; so from

[   {name: "Joe", age: 22},   {name: "Kevin", age: 24},   {name: "Peter", age: 21} ] 

perform the join method only on the name attribute, to achieve the same output as before.

Currently I have the following function:

function joinObj(a, attr){   var out = [];    for (var i = 0; i < a.length; i++){     out.push(a[i][attr]);   }    return out.join(", "); } 

There’s nothing wrong with that code, it works, but all of a sudden I’ve gone from a simple, succinct line of code to a very imperative function. Is there a more succinct, ideally more functional way of writing this?

like image 558
jackweirdy Avatar asked May 17 '13 11:05

jackweirdy


People also ask

How do you use join on array of objects in JavaScript?

join() The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

How do you join values in objects?

To merge objects into a new one that has all properties of the merged objects, you have two options: Use a spread operator ( ... ) Use the Object. assign() method.

How do you combine two objects in an array?

We can use the spread operator on arrays within an array literal( [] ) to merge them. Let's see it with an example. First, we will take two arrays, arr1 and arr2 . Then merge the arrays using the spread operator( ... ) within an array literal.

How do you turn an array into a string?

To convert a JavaScript array into a string, you can use the built-in Array method called toString . Keep in mind that the toString method can't be used on an array of objects because it will return [object Object] instead of the actual values.


1 Answers

If you want to map objects to something (in this case a property). I think Array.prototype.map is what you're looking for if you want to code functionally.

console.log([       {name: "Joe", age: 22},       {name: "Kevin", age: 24},       {name: "Peter", age: 21}     ].map(function(elem){         return elem.name;     }).join(","));

In modern JavaScript:

console.log([       {name: "Joe", age: 22},       {name: "Kevin", age: 24},       {name: "Peter", age: 21}     ].map(e => e.name).join(","));

(fiddle)

If you want to support older browsers, that are not ES5 compliant you can shim it (there is a polyfill on the MDN page above). Another alternative would be to use underscorejs's pluck method:

var users = [       {name: "Joe", age: 22},       {name: "Kevin", age: 24},       {name: "Peter", age: 21}     ]; var result = _.pluck(users,'name').join(",") 
like image 61
Benjamin Gruenbaum Avatar answered Sep 17 '22 17:09

Benjamin Gruenbaum