英文:
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.
_.invoke = function(collection, methodName) {
var result = [];
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
methodName.call(collection[i]);
var value = collection[i][methodName];
result.push(value);
}
}
return result;
}
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.
_.invoke = function(collection, methodName) {
var result = [];
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
methodName.call(collection[i])
var value = collection[i][methodName]
result.push(value)
}
}
return result
}
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
_.invoke = function(collection, methodName, ...args) {
if (!Array.isArray(collection)) {
return [];
}
const out = [];
for(const item of collection){
if(typeof item[methodName] === 'function')
out.push(item[methodName].apply(item, args));
}
}
return out;
}
const collection = [...];
const allHaveMethod = _.invoke(collection, 'method', 'arg1', 'arg2').length === collection.length;
英文:
Here you can invoke with arguments.
_.invoke = function(collection, methodName, ...args) {
if (!Array.isArray(collection)) {
return [];
}
const out = [];
for(const item of collection){
if(typeof item[methodName] === 'function')
out.push(item[methodName].apply(item, args));
}
}
return out;
}
To test that all items have a method:
const collection = [...];
const allHaveMethod = _.invoke(collection, 'method', 'arg1', 'arg2').length === collection.length;
答案2
得分: 1
以下是您要的翻译内容:
你的意思是这样吗?
const myArr = [
{ cons: function(args) { return args; } },
{ cons: function(args) { return args["bla"]; } },
];
const _ = {};
_.invoke = (collection, methodName, ...args) => {
const result = [];
if (!Array.isArray(collection)) return result;
for (let i = 0, len = collection.length; i < len; i++) {
const item = collection[i];
const method = item[methodName];
if (typeof method === 'function') {
result.push(method.apply(item, args));
}
}
return result;
};
const res = _.invoke(myArr, "cons", {
"bla": "hello"
});
console.log(res);
这是您提供的JavaScript代码的翻译版本。
英文:
You mean something like this?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const myArr = [
{ cons:function(args) { return args } },
{ cons:function(args) { return args["bla"] } },
]
const _ = {};
_.invoke = (collection, methodName, ...args) => !Array.isArray(collection) ? [] : collection
.filter(item => typeof item[methodName] === 'function')
.map(item => item[methodName].apply(item, args));
const res = _.invoke(myArr,"cons",{"bla":"hello"})
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 -->
const myArr = [
{ cons: function(args) { return args; } },
{ cons: function(args) { return args["bla"]; } },
];
const _ = {};
_.invoke = (collection, methodName, ...args) => {
const result = [];
if (!Array.isArray(collection)) return result;
for (let i = 0, len = collection.length; i < len; i++) {
const item = collection[i];
const method = item[methodName];
if (typeof method === 'function') {
result.push(method.apply(item, args));
}
}
return result;
};
const res = _.invoke(myArr, "cons", {
"bla": "hello"
});
console.log(res);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论