5

I have a Javascript object, which contains other objects. Each of the contained objects has got a name property.

What I would like to do now is to concatenate these name properties to one String. However, I want these Strings separated by commas and an 'and' before the last String.

For better understanding, the object i want to iterate over would look like this:

var objects = {
    o1: {name: 'name1'}, 
    o2: {name: 'name2'},
    o3: {name: 'name3'}
};

Now the String I would like to have in the end would be: 'Concatenation of name1, name2 and name3'

What I've tried so far was using angular.forEach:

var myString = 'Concatenation of ';

angular.forEach(objects, function(o) {
    myString += o.name + ', ';
}

Not hard to notice, that my String would become 'Concatenation of name1, name2, name3, '.

So the real question would be how I can check at which position in my object I am and reacting appropiately by concatenating 'and' instead of a comma or no comma at all. How can I do that?

1
  • angular.forEach(objects, function(o,i,r) { ... if(r[i+1]) myString+=", "; but better is to do myString=angular.map(objects, function(o) { return o.name;}).join(", "); Commented Sep 24, 2015 at 9:11

3 Answers 3

6

Use a traditional for loop:

var myString = 'Concatenation of ';
var i = 0;
var keys = Object.keys(objects);
for (i =0; i<keys.length; i++) {
   if (i > 0)
      myString += (i < keys.length - 1) ? ', ' : ' and ';
   myString += objects[keys[i]].name;
}
Sign up to request clarification or add additional context in comments.

1 Comment

objects is an object and not an array so there is no length property. Also where is o coming from?
2

Here's a short and sweet solution using jQuery:

Code:

var myString = 'Concatenation of ';
var arr = $.map(objects, function(o) { return o["name"]; })
// append 'and ' to the last element before hand.
arr[arr.length - 1] = "and "+arr[arr.length - 1]; 
console.log(myString+arr.join(", "));
// Concantenation of name1, name2, and name3

Demo Fiddle

Update

Here's the updated code to not include the oxford comma:

var myString = 'Concatenation of ';
var arr = $.map(objects, function(o) { return o["name"]; })
var lastElem = " and " +arr.pop();
console.log(myString+arr.join(", ")+lastElem);
// Concantenation of name1, name2 and name3

Updated Fiddle

3 Comments

your solution adds a comma before the 'and'. You would have to merge the last element and the element before the last element somehow.
That comma is called an 'oxford comma'. I thought that made sense here! In any case, I'll update my solution as per your requirement.
Well, who said stackoverflow couldn't teach more than just programming - I didn't know about the Oxford Comma :) However, I'm using this code for an app thats only released in German, where there's no such thing as the serial comma, so the change would be required for my use case. Thanks for the edit and the help, much appreciated.
1

Try something like

var myString = 'Concatenation of ';
var i = 1;
angular.forEach(objects, function(o) {
  i += 1;
  if(i === Object.keys(objects).length) {
     myString = myString.subString(1, myString .length-1);
     myString += ' and '+ o.name;
   } else {
     myString += o.name + ', ';
   }

}

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.