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

JavaScript - Insert an element to an array without affecting the indexes? - Stack Overflow

programmeradmin2浏览0评论

I'm working on a problem like this for sometime now, trying to figure out what to do with the indexes. Here's the problem:

I have a string: thisIsGreat The basic operations used are bellow:

var str = "thisIsGreat"; 
str = str.split(''); //Making it an array
.....
str = str.join(''); //String it back again
console.log(str);

Desired Output: ["t","h","i","s"," ","I","s"," ","G","r","e","a",t"]

I'm having hard time inserting spaces between those words without affecting the indexes. I tried something like this, but it didn't work.

for (var i = 0; i < str.length; i++){
     if (str[i]+1 == (str[i].toUpperCase())+1)
      str[i] = " ";
}

Is there a way to fix this? Any thoughts?

I'm working on a problem like this for sometime now, trying to figure out what to do with the indexes. Here's the problem:

I have a string: thisIsGreat The basic operations used are bellow:

var str = "thisIsGreat"; 
str = str.split(''); //Making it an array
.....
str = str.join(''); //String it back again
console.log(str);

Desired Output: ["t","h","i","s"," ","I","s"," ","G","r","e","a",t"]

I'm having hard time inserting spaces between those words without affecting the indexes. I tried something like this, but it didn't work.

for (var i = 0; i < str.length; i++){
     if (str[i]+1 == (str[i].toUpperCase())+1)
      str[i] = " ";
}

Is there a way to fix this? Any thoughts?

Share Improve this question edited May 3, 2016 at 18:38 incalite asked May 3, 2016 at 18:33 incaliteincalite 3,2173 gold badges28 silver badges32 bronze badges 4
  • you need to use the same array? can't you generate another one as result? – Hugo S. Mendes Commented May 3, 2016 at 18:35
  • Yes, I can generate a new array if needed..Any ideas? – incalite Commented May 3, 2016 at 18:36
  • For (var in something) is used for getting all properties of objects and not elements of array. What exactly are you trying to achieve? – It-Z Commented May 3, 2016 at 18:38
  • @It-Z My mistake, used a wrong loop..Edited now.. – incalite Commented May 3, 2016 at 18:39
Add a ment  | 

5 Answers 5

Reset to default 8

You could use a replace with positive lookahead.

var str = "thisIsGreat";
   
str = str.replace(/(?=[A-Z])/g, ' ').split('');
document.write('<pre>' + JSON.stringify(str, 0, 4) + '</pre>');

Your question is not very clear, if you just want to insert a space before each uppercase letter, this is fairly easy with regexes:

r = "thisIsGreat".replace(/[A-Z]/g, " $&");
alert(r)

using your own logic

var str = "thisIsGreat"; 
str = str.split(''); //Making it an array
var result = [];
for (var i in str){
 if (str[i]+1 == (str[i].toUpperCase())+1){
  result.push(" ");
  result.push(str[i]);
 }else{
  result.push(str[i]);
  }
}
result = result.join(''); //String it back again
console.log(result);

if you don't want the result as string just remove the join in the end.

https://jsfiddle/Ldp58onu/2/

Just reverse your for loop.

for (var i = str.length - 1; i >= 0; i--){
     if (str[i]+1 == (str[i].toUpperCase())+1)
      str[i] = " ";
}
'thisIsGreat'.replace(/([A-Z])/g,' $1').split('').join('", "')
//Output: "t", "h", "i", "s", " ", "I", "s", " ", "G", "r", "e", "a", "t"
发布评论

评论列表(0)

  1. 暂无评论