Perlish map()函数 [英] Perlish map() function
问题描述
Perl'的map()函数是一个功能强大的快捷方式,可以完成许多循环结束数组并执行X<操作。
下面是一个模拟类似功能的快速黑客。
我已经使用了几次并发现它非常方便。
有什么想法吗?
< script type =" text / javascript">
功能图(func){
var i,j,o;
var results = [];
if(typeof(func)==" string"){
func = new Function(''$ _'',func);
}
for(i = 1; i< arguments.length; i ++) {
o = arguments [i];
if(typeof(o)==" object"&& o.length){
for(j = 0; j< o.length; j ++){
results [results.length] = func(o [j]);
}
}
else if(typeof(o)==" object"){
for(j in o){
结果[results.length] = func(o [j]);
}
}
else {
结果[results.length] = func(o);
}
}
返回结果;
}
var o = {''a'':''a1'',''b'':''b1'',''c'':''c1''};
var a = ['''',''b'',''c''];
map(" alert($ _)",o ,a,测试, );
map(function(o){alert(" Value:" + o);},o,a," test");
map (" alert($ _);",map(" return $ _。toUpperCase();",o,a," test"));
< / script> ;
PS:使用$ _作为标识符是从Perl继承的,如果你不知道
不熟悉它。 />
-
Matt Kruse
http://www.JavascriptToolbox.com
http:// www .AjaxToolbox.com
Perl''s map() function is a powerful shortcut to accomplish many "loop over
an array and do X" operations.
Below is a quick hack to simulate similar functionality.
I''ve used it a few times and find it to be quite handy.
Any thoughts?
<script type="text/javascript">
function map(func) {
var i,j,o;
var results = [];
if (typeof(func)=="string") {
func = new Function(''$_'',func);
}
for (i=1; i<arguments.length; i++) {
o = arguments[i];
if (typeof(o)=="object" && o.length) {
for (j=0; j<o.length; j++) {
results[results.length] = func(o[j]);
}
}
else if (typeof(o)=="object") {
for (j in o) {
results[results.length] = func(o[j]);
}
}
else {
results[results.length] = func(o);
}
}
return results;
}
var o = { ''a'':''a1'', ''b'':''b1'', ''c'':''c1'' };
var a = [ ''a'',''b'',''c'' ];
map( "alert($_)", o , a, "test" );
map( function(o){ alert("Value:"+o); } , o , a, "test" );
map( "alert($_);", map( "return $_.toUpperCase();" , o, a, "test") );
</script>
PS: The use of $_ as an identifier is carried over from Perl, in case you
aren''t familiar with it.
--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
推荐答案
_'',func);
}
for(i = 1; i< arguments.length; i ++){
o = arguments [i];
if(typeof(o)==" object"&& o.length){
for(j = 0; j< o.length; j ++){
results [results.length] = func (o [j]);
}
}
else if(t ypeof(o)==" object"){
for(j in o){
results [results.length] = func(o [j]);
}
}
else {
results [results.length] = func(o);
}
}
返回结果;
}
var o = {'' a'':''a1'',''b'':''b1'',''c'':''c1''};
var a = [''a '',''b'',''c''];
map(" alert(
_'',func);
}
for (i=1; i<arguments.length; i++) {
o = arguments[i];
if (typeof(o)=="object" && o.length) {
for (j=0; j<o.length; j++) {
results[results.length] = func(o[j]);
}
}
else if (typeof(o)=="object") {
for (j in o) {
results[results.length] = func(o[j]);
}
}
else {
results[results.length] = func(o);
}
}
return results;
}
var o = { ''a'':''a1'', ''b'':''b1'', ''c'':''c1'' };
var a = [ ''a'',''b'',''c'' ];
map( "alert(
_)", o,a,test );
map(function(o){alert(" Value:" + o);},o,a," test");
map (" alert(
_)", o , a, "test" );
map( function(o){ alert("Value:"+o); } , o , a, "test" );
map( "alert(
_);",map(" return
_);", map( "return
这篇关于Perlish map()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!