admin管理员组文章数量:1391969
I Have an array say
['(','C','1','2',')']
Now I want to trim data from array beginning from indexOf('C') + 2
,If it is a digit I need to remove it from array.. So for the above example final array has to be
['(','C','1',')']
For example if I have ['(','C','1','2','3','*',')']
I want it to be trimmed to ['(','C','1','*',')']
, After element 'C' only one numeral is allowed.
I know I can traverse the array by getting the indexOf('C') and then checking each element for numeric.. but help me with some efficient and better way. like using splice or something.
I Have an array say
['(','C','1','2',')']
Now I want to trim data from array beginning from indexOf('C') + 2
,If it is a digit I need to remove it from array.. So for the above example final array has to be
['(','C','1',')']
For example if I have ['(','C','1','2','3','*',')']
I want it to be trimmed to ['(','C','1','*',')']
, After element 'C' only one numeral is allowed.
I know I can traverse the array by getting the indexOf('C') and then checking each element for numeric.. but help me with some efficient and better way. like using splice or something.
Share Improve this question edited Mar 25, 2013 at 5:50 000 27.3k10 gold badges74 silver badges103 bronze badges asked Mar 25, 2013 at 5:33 GarryGarry 3061 gold badge7 silver badges24 bronze badges 3- Sounds like you're looking for Array.slice() ?? – adeneo Commented Mar 25, 2013 at 5:35
-
4
Traversing the array is not anymore inefficient than using
.splice()
or something.... Since you would have to have used.indexOf()
to get the indices to do the splice. Another method is joining the array back into a String and performing regex matching. – Amy Commented Mar 25, 2013 at 5:36 - @sweetamylase—joining and using a regular expression may work in trivial cases (per the OP), but where the data is a random string it's very difficult. Regular expressions aren't good for parsing irregular data (even fairly ordered irregular data, like markup). – RobG Commented Mar 25, 2013 at 5:53
3 Answers
Reset to default 3If the position from where you want to 'trim' is known, you could use filter
here, like.:
var a = ['(','C','1','2','3','*',')']
.filter( function(a){
this.x += 1;
return this.x<=2 ? a : isNaN(+a);}, {x:-1}
);
Which could lead to this Array
extension:
Array.prototype.trimNumbersFrom = function(n){
return this.filter( function(a){
this.x += 1;
return this.x<=n ? a : isNaN(+a);
}, {x:-1}
);
};
//=> usage
var a = ['(','C','1','2','3','*',')'].trimNumbersFrom(2);
//=> ["(", "C", "1", "*", ")"]
See also ...
var a = ['(','C', 'a', '8','2',')'].join("").split("C");
var nPos = a[1].search(/[0-9]/g);
var firstNumber = a[1][nPos];
var b = a[1].split(n);
// rebuild
a = a[0] + "C" + b[0] + firstNumber + b[1].replace(/[0-9]/g, "");
Not tested thoroughly but for your case it works.
You can use of isNaN() function which returns false if its a valid number or true if it's not a number
var str = ['(','C','1','2','3','4','*',')']; // Your Input
var temp = [],count = 0;
for(var i=0;i<str.length;i++)
{
if(i<str.indexOf('C') + 2)
{
temp[count] = str[i];
count++;
}
else
{
if(isNaN(str[i]) == true)
{
temp[count] = str[i];
count++;
}
}
}
str = temp;
alert(str);
LIVE DEMO
本文标签: javascripttrim Array in JSStack Overflow
版权声明:本文标题:javascript - trim Array in JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744735443a2622299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论