1
var student = [{
    "fname": "Jackie",
    "mname": "Lee",
    "lname": "Li"
}, {
    "fname": "Ken",
    "mname": "Ryu",
    "lname": "Sha"
}];

for (var i = 0; i < student.length; i++) {
    console.log(student[i].fname + " " + student[i].mname + ". " + student[i].lname ? student[i].fname + " " + student[i].mname + ". " + student[i].lname + " , " : " ");
}

I am trying to combine names in the a td these names are from an array. I was able to make the names combine by above code. problem is on the last name there is still a , just want to get rid of the comma when there are no more names that will follow. I am bugged by this simple comma for hours. any idea is appreaciate.

FIDDLE

7
  • console.log(student[i].fname + " " + student[i].mname + ". " + student[i].lname + ((i < student.length -1) ? ", " : " ")); Commented Jun 11, 2015 at 11:09
  • 3
    What about using .join()? Fiddle. Commented Jun 11, 2015 at 11:10
  • I do just want to mention that the use of tertiary operator in this piece of code is ill-advised. It makes this string of code illegible. Rather spread out over multiple lines and store in a variable than using this many nested operators. Commented Jun 11, 2015 at 11:14
  • @somethinghere are you talking about markai's suggestion? Commented Jun 11, 2015 at 11:21
  • 2
    @Pekka I am pointing at your original code's use of the tertiary operator. Its recommended to not use it in the middle of a string concatenation as it looks really confusing. Try to separate everything out in steps first - create a variable to store your string, add the first thing, then add the next, then use a tertiary operator to add one more etc... Then log the string stored in the variable. Do all these on separate lines with the += operator. It will make your code easier to parse, errors easier to spot, and solutions easier to find. Commented Jun 11, 2015 at 11:25

4 Answers 4

6

One easy way is to build up an array and then using .join(", "):

var student = [{
  "fname": "Jackie",
  "mname": "Lee",
  "lname": "Li"
}, {
  "fname": "Ken",
  "mname": "Ryu",
  "lname": "Sha"
}];

var names = [];
for (var i = 0; i < student.length; i++) {
  names.push(student[i].fname + " " + student[i].mname + ". " + student[i].lname);
}
snippet.log(names.join(", "));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

It can even be more concise with Array#map:

var student = [{
  "fname": "Jackie",
  "mname": "Lee",
  "lname": "Li"
}, {
  "fname": "Ken",
  "mname": "Ryu",
  "lname": "Sha"
}];

snippet.log(student.map(function(entry) {
  return entry.fname + " " + entry.mname + ". " + entry.lname;
}).join(", "));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

3

Try this bit of code

var student = [{
  "fname": "Jackie",
  "mname": "Lee",
  "lname": "Li"
}, {
  "fname": "Ken",
  "mname": "Ryu",
  "lname": "Sha"
}]

for (var i = 0; i < student.length; i++) {
  var out = student[i].fname + " " + student[i].mname + ". " + student[i].lname;
  if (i < student.length - 1) {
    out += ", ";
  } else {
    out += " ";
  }
  snippet.log(out);
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

1 Comment

This answer has already been given. The drawback is the readability. The ternary operator in the statement obfuscates the code a little bit.
3

If I understand your question right than this should do it:

for(var i = 0 ; i < student.length; i++){
  var name = student[i].fname + " " + student[i].mname + ". " + student[i].lname ?  student[i].fname + " " + student[i].mname + ". " + student[i].lname: " ";
  if (i < (student.length - 1)) {
      name += ',';
  }
}

Comments

2

Try this jsfiddle

you should use like this:

var student = [{
     "fname": "Jackie",
         "mname": "Lee",
         "lname": "Li"
 }, {
     "fname": "Ken",
         "mname": "Ryu",
         "lname": "Sha"
 }]

 for (var i = 0; i < student.length; i++) {
     console.log(student[i].fname + " " + student[i].mname + ". " + student[i].lname + ((i < student.length - 1) ? ", " : " "));
 }

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.