0

I have this solved, but I am looking to see if there is a better way to do this. Fiddle - https://jsfiddle.net/143h9wrq/ So what I do is check to see if there are 2+ Ids, then turn the string into an array, take the last array item and add 'and ', put it back together and display it.

I am looking to see if there is a better way to condense that if. I am using Angular in the project, not in the example. Basicly listArr would be $scope.listArr and issues is $user.issues.

JS:

var issuesId =[1,2,3,4]
var issues = "issue one, issue two, issue three, issue four";

if (issuesId.length >= 2) {
  var str = issues.split(', ');
  var addAnd = str.pop();
  addAnd = 'and ' + addAnd;
  str.push(addAnd);
  var listArr = str.join(', ');
  document.getElementById('text').innerHTML = listArr;
}
else {
  var listArr = issues;
  document.getElementById('text').innerHTML = listArr;
}

HTML:

<p>{{listArr}}</p>
8
  • I think your approach is more than valid Commented Apr 12, 2016 at 15:08
  • The only problem I see with your approach is that you end up with a sentence that is not correct as it will insert a , before the and when none is needed. Commented Apr 12, 2016 at 15:12
  • This question is tagged under angularjs - And that approach is nothing like an angularjs approach Commented Apr 12, 2016 at 15:14
  • I think it's because the code is used in Angular but the jsfiddle is not so the code displayed is the one from jsfiddle instead of the angular code. I imagine in the angular code the value is just assigned to listArr on the scope. Commented Apr 12, 2016 at 15:15
  • Maybe your particular use case requires a comma with only two items but this is usually incorrect. Commented Apr 12, 2016 at 15:15

4 Answers 4

3

The shortest solution will be with regular expressions:

var s = "a, b, c";

s.replace(/ ([^,]*)$/, " and $1"); // "a, b, and c"

or:

s.replace(/, ([^,]*)$/, " and $1"); // "a, b and c"
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect thanks. If you have time, would you mind putting a brief overview on what does what. I have used RegEx before and love regex101.com, but always forget what does what :]
3

Why not change the last element of the array, directly from the array, without poping and pushing it back again, like this: str[str.length - 1] = 'and ' + str[str.length - 1]?

I've also trimmed down a bunch of code lines:

var issuesId = [1, 2, 3, 4]
var issues = "issue one, issue two, issue three, issue four";

var listArr = issues;

if (issuesId.length >= 2) {
    var str = issues.split(', ');
    str[str.length - 1] = 'and ' + str[str.length - 1];
    listArr = str.join(', ');
}

document.getElementById('text').innerHTML = listArr;

Comments

0

More functional style. Try this:

var issuesId =[1,2,3,4]
var issues = "issue one, issue two, issue three, issue four";

if(issuesId.length >= 2){
  var arr = issues.split(',');

  arr = arr.map(function(phrase, index){
    if(index < arr.length - 1)
     phrase+=' and';
    return phrase;
  })
  issues = arr.join(', ');
}

console.log(issues);

http://codepen.io/gpincheiraa/pen/bpLJdv

Comments

0

Generally, I'd wrap the string manipulation into a function, separating it from the actual dom manipulation:

function mkStr(issues) {
   var delimeter = ', ';
   if (issues.length < 2) {
      return issues;
   } else {
     let parts = issues.split(delimeter);
     let last = "and " + parts.pop();
     parts.push(last);
     return parts.join(delimeter);
   }
}

document.getElementById('text').innerHTML = mkStr(issues);

mkStr("a, b, c"); // "a, b, and c"

But, maybe, you want rather to have "a, b, and c" as an output - this would be more natural somehow. Then you could do something like this:

function mkStr2(issues) {
   var delimeter = ', ';
   if (issues.length < 2) {
      return issues;
   } else {
     let parts = issues.split(delimeter);
     let last = parts.pop();
     return parts.join(delimeter) +
            " and " +
            last;
   }
}

mkStr2("a, b, c"); // "a, b, and c"

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.