将从startIndex加入阵列ENDINDEX到 [英] Join Array from startIndex to endIndex
本文介绍了将从startIndex加入阵列ENDINDEX到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想问问是否有某种效用函数它提供阵列连接,同时提供一个指标。也许jQuery的原型提供了这一点,如果没有,我会写我自己:)
I wanted to ask if there is some kind of utility function which offers array joining while providing an index. Maybe Prototype of jQuery provides this, if not, I will write it on my own :)
我希望是像
var array= ["a", "b", "c", "d"];
function Array.prototype.join(seperator [, startIndex, endIndex]){
// code
}
让array.join( - ,1,2)将返回B-C
so that array.join("-", 1, 2) would return "b-c"
有没有这样的效用函数在pretty常见的JavaScript库?
Is there this kind of utility function in an pretty common Javascript Library?
问候
Wormi
Regards Wormi
推荐答案
它的工作原理本地
["a", "b", "c", "d"].slice(1,3).join("-") //b-c
如果你想让它表现得像你的定义,你可以用这种方式:
If you want it to behave like your definition you could use it that way:
Array.prototype.myJoin = function(seperator,start,end){
if(!start) start = 0;
if(!end) end = this.length - 1;
end++;
return this.slice(start,end).join(seperator);
};
var arr = ["a", "b", "c", "d"];
arr.myJoin("-",2,3) //c-d
arr.myJoin("-") //a-b-c-d
arr.myJoin("-",1) //b-c-d
这篇关于将从startIndex加入阵列ENDINDEX到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文