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 mas 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 bee '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 ma or no ma at all. How can I do that?
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 mas 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 bee '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 ma or no ma at all. How can I do that?
-
angular.forEach(objects, function(o,i,r) { ... if(r[i+1]) myString+=", ";
but better is to domyString=angular.map(objects, function(o) { return o.name;}).join(", ");
– dandavis Commented Sep 24, 2015 at 9:11
3 Answers
Reset to default 6Use 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;
}
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 ma:
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
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 + ', ';
}
}