最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angularjs - Javascript: Iterating over object and concatenate strings - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question asked Sep 24, 2015 at 9:02 greyhairredbeargreyhairredbear 8023 gold badges12 silver badges31 bronze badges 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(", "); – dandavis Commented Sep 24, 2015 at 9:11
Add a ment  | 

3 Answers 3

Reset to default 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;
}

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 + ', ';
   }

}
发布评论

评论列表(0)

  1. 暂无评论