2

I use the following code to add space inside the inverted commas for some value ,there is nicer/elegant way to do it ?

text: x.fullPath + "                           " + x.name

2 Answers2

3

You can create a function which return the string joining the two values with the given number of space.

text: addSpace(x.fullPath, x.name, 5)

Will give you a new string joining these two values with 5 spaces.

You can use this function or

function addSpace(stringA, stringB, spaces){
   return x.fullPath + Array(spaces).join(" ") + x.name;
}
Community
  • 1
  • 1
void
  • 36,090
  • 8
  • 62
  • 107
1

A simple way would be to use this:

text: x.fullPath + Array(30+1).join(" ") + x.name

where 30 is the amount of spaces


This would be a function:

// text: addSpace(x.fullPath, x.name, 5)

function addSpace(stringA, stringB, spaceCount) {
  var spaces = ""
  for (var i = 0; i < spaceCount; ++i) {
    spaces += " "
  }
  return stringA + spaces + stringB
}

// Demo
alert(addSpace("test1", "test2", 30))
CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • Array.join puts spaces between the items in the array, which means `Array(30).join(" ")` will produce 29 spaces. – zzzzBov Dec 28 '15 at 13:57
  • @zzzzBov I overlooked that, thank you! - http://stackoverflow.com/a/1877479/4339170 – CoderPi Dec 28 '15 at 14:00