重写与underscore库启发的invoke函数时遇到问题。

huangapple go评论105阅读模式
英文:

Having a problem with rewriting invoke function inspired by underscore library

问题

I'm a beginner and trying to rewrite an underscore function _.invoke.
I'm trying to make the function so it returns an array with the results of calling the method on each value in the collection.

  1. _.invoke = function(collection, methodName) {
  2. var result = [];
  3. if (Array.isArray(collection)) {
  4. for (let i = 0; i < collection.length; i++) {
  5. methodName.call(collection[i]);
  6. var value = collection[i][methodName];
  7. result.push(value);
  8. }
  9. }
  10. return result;
  11. }

I think my problem lays in this line:

methodName.call(collection[i]) - would like to invoke method on an object collection[i] but I would like to pass some arguments should they be included in the unit test.

I've tried so far utilizing the test: typeof(methodName) === "function" and writing a function that would test whether the method is a function.

英文:

I'm a beginner and trying to rewrite an underscore function _.invoke.
I'm trying to make the function so it returns an array with the results of calling the method on each value in the collection.

  1. _.invoke = function(collection, methodName) {
  2. var result = [];
  3. if (Array.isArray(collection)) {
  4. for (let i = 0; i < collection.length; i++) {
  5. methodName.call(collection[i])
  6. var value = collection[i][methodName]
  7. result.push(value)
  8. }
  9. }
  10. return result
  11. }

I think my problem lays in this line:

methodName.call(collection[i]) - would like to invoke method on an object collection[i] but I would like to pass some arguments should they be included in the unit test.

I've tried so far utilizing the test : typeof(methodName) === "function" and writing a function that would test whether the method is a function.

答案1

得分: 1

  1. _.invoke = function(collection, methodName, ...args) {
  2. if (!Array.isArray(collection)) {
  3. return [];
  4. }
  5. const out = [];
  6. for(const item of collection){
  7. if(typeof item[methodName] === 'function')
  8. out.push(item[methodName].apply(item, args));
  9. }
  10. }
  11. return out;
  12. }
  13. const collection = [...];
  14. const allHaveMethod = _.invoke(collection, 'method', 'arg1', 'arg2').length === collection.length;
英文:

Here you can invoke with arguments.

  1. _.invoke = function(collection, methodName, ...args) {
  2. if (!Array.isArray(collection)) {
  3. return [];
  4. }
  5. const out = [];
  6. for(const item of collection){
  7. if(typeof item[methodName] === 'function')
  8. out.push(item[methodName].apply(item, args));
  9. }
  10. }
  11. return out;
  12. }

To test that all items have a method:

  1. const collection = [...];
  2. const allHaveMethod = _.invoke(collection, 'method', 'arg1', 'arg2').length === collection.length;

答案2

得分: 1

以下是您要的翻译内容:

  1. 你的意思是这样吗
  2. const myArr = [
  3. { cons: function(args) { return args; } },
  4. { cons: function(args) { return args["bla"]; } },
  5. ];
  6. const _ = {};
  7. _.invoke = (collection, methodName, ...args) => {
  8. const result = [];
  9. if (!Array.isArray(collection)) return result;
  10. for (let i = 0, len = collection.length; i < len; i++) {
  11. const item = collection[i];
  12. const method = item[methodName];
  13. if (typeof method === 'function') {
  14. result.push(method.apply(item, args));
  15. }
  16. }
  17. return result;
  18. };
  19. const res = _.invoke(myArr, "cons", {
  20. "bla": "hello"
  21. });
  22. console.log(res);

这是您提供的JavaScript代码的翻译版本。

英文:

You mean something like this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const myArr = [
  2. { cons:function(args) { return args } },
  3. { cons:function(args) { return args[&quot;bla&quot;] } },
  4. ]
  5. const _ = {};
  6. _.invoke = (collection, methodName, ...args) =&gt; !Array.isArray(collection) ? [] : collection
  7. .filter(item =&gt; typeof item[methodName] === &#39;function&#39;)
  8. .map(item =&gt; item[methodName].apply(item, args));
  9. const res = _.invoke(myArr,&quot;cons&quot;,{&quot;bla&quot;:&quot;hello&quot;})
  10. console.log(res)

<!-- end snippet -->

Here is a more performant version if you need speed over a lot of entries

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const myArr = [
  2. { cons: function(args) { return args; } },
  3. { cons: function(args) { return args[&quot;bla&quot;]; } },
  4. ];
  5. const _ = {};
  6. _.invoke = (collection, methodName, ...args) =&gt; {
  7. const result = [];
  8. if (!Array.isArray(collection)) return result;
  9. for (let i = 0, len = collection.length; i &lt; len; i++) {
  10. const item = collection[i];
  11. const method = item[methodName];
  12. if (typeof method === &#39;function&#39;) {
  13. result.push(method.apply(item, args));
  14. }
  15. }
  16. return result;
  17. };
  18. const res = _.invoke(myArr, &quot;cons&quot;, {
  19. &quot;bla&quot;: &quot;hello&quot;
  20. });
  21. console.log(res);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 18:24:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定