重新创建JavaScript的减少功能 [英] Recreating JavaScript's reduce function

查看:123
本文介绍了重新创建JavaScript的减少功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这帮助我理解了很多,但我有一个小问题:
如何重新创建Underscore.js _.reduce方法?
当我插入数组时,它正常工作,但对象不起作用。我弄不明白。对于我应该改变的内容,我将不胜感激。我评论了我认为应该改变的地方,但我很困惑我需要改变它。我也创建了我自己的每个功能。

你可以在这里找到我的代码: http:// goo。 gl / 6RU9Bc



任何建议都会非常有帮助。谢谢!!



这是我到目前为止:

  var myArray = [1,2,3,4,5]; 

var myObject = {
num1:1,
num2:2,
num3:3
};

函数each(collection,callback){
if(Array.isArray(collection)){
for(var i = 0,l = collection.length; i callback(collection [i]);
};
} else if(collection ===object){
for(var collection in collection){
callback(collection [prop]);
};
}
}

函数multiply(num,num2){
return num * num2;


函数reduce(collection,callback,accumulator){
each(collection,function(element){
if(accumulator === undefined){
return accumulator = element; //这里是问题吗?为什么?我不明白。
} else {
return accumulator = callback(accumulator,element);
};
});

返回累加器;
};

console.log(reduce(myArray,multiply)); // 120
console.log(reduce(myArray,multiply,5)); // 160
console.log(reduce(myObject,multiply)); //返回undefined
console.log(reduce(myObject,multiply,5)); //返回5


解决方案

正如@Yeldar Kurmangaliyev ,你需要改变它:

  if(collection ===object)
  if(typeof(集合)===object)

以下是固定代码:

  var myArray = [1,2,3,4,5]; 

var myObject = {
num1:1,
num2:2,
num3:3
};

函数each(collection,callback){
if(Array.isArray(collection)){
for(var i = 0,l = collection.length; i callback(collection [i]);
};
} else if(typeof(collection)===object){
for(var collection in collection){
callback(collection [prop]);
};
}
}

函数multiply(num,num2){
return num * num2;


函数reduce(collection,callback,accumulator){
each(collection,function(element){
if(accumulator === undefined){
return accumulator = element; //这里是问题吗?为什么?我不明白。
} else {
return accumulator = callback(accumulator,element);
};
});

返回累加器;
};

console.log(reduce(myArray,multiply)); // 120
console.log(reduce(myArray,multiply,5)); // 600
console.log(reduce(myObject,multiply)); // 6
console.log(reduce(myObject,multiply,5)); // 30


I'm trying to recreate the reduce method but as the original function.

This helped me to understand a lot but I have one little issue: How to re-create Underscore.js _.reduce method? When I plug in arrays it works right, but objects don't work. I get undefined. I would appreciate advice on what I should change. I commented where I think I should change something but I'm confused exactly what I need to change it to. I also created my own each function.

You can find my code here: http://goo.gl/6RU9Bc

Any advice would be super helpful. Thank you!!

This is what I have so far:

var myArray=[1,2,3,4,5];

var myObject={
  num1:1,
  num2:2,
  num3:3
};

function each(collection, callback){
  if(Array.isArray(collection)) {
    for(var i=0, l=collection.length; i<l; i++){
      callback(collection[i]); 
    };
  }else if(collection === "object") {
    for(var prop in collection){
       callback(collection[prop]);
    };
  }
}

function multiply(num, num2){
  return num*num2;
}

function reduce(collection, callback, accumulator){
  each(collection, function(element){
    if(accumulator === undefined) {
      return accumulator = element; // is the problem here? Why? I don't understand.
    }else {
      return accumulator = callback(accumulator, element);
    };   
});

return accumulator;
};

console.log(reduce(myArray, multiply)); // 120
console.log(reduce(myArray, multiply, 5)); // 160
console.log(reduce(myObject, multiply)); // returns undefined
console.log(reduce(myObject, multiply, 5)); // returns 5

解决方案

As pointed out by @Yeldar Kurmangaliyev, you need to change this:

if(collection === "object")

to:

if(typeof(collection) === "object")

Here is the fixed code:

var myArray=[1,2,3,4,5];

var myObject={
  num1:1,
  num2:2,
  num3:3
};

function each(collection, callback){
  if(Array.isArray(collection)) {
    for(var i=0, l=collection.length; i<l; i++){
      callback(collection[i]); 
    };
  }else if(typeof(collection) === "object") {
    for(var prop in collection){
       callback(collection[prop]);
    };
  }
}

function multiply(num, num2){
  return num*num2;
}

function reduce(collection, callback, accumulator){
  each(collection, function(element){
    if(accumulator === undefined) {
      return accumulator = element; // is the problem here? Why? I don't understand.
    }else {
      return accumulator = callback(accumulator, element);
    };   
});

return accumulator;
};

console.log(reduce(myArray, multiply)); // 120
console.log(reduce(myArray, multiply, 5)); // 600
console.log(reduce(myObject, multiply)); // 6
console.log(reduce(myObject, multiply, 5)); // 30

这篇关于重新创建JavaScript的减少功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆