在 javascript 中推送回调值
pushing a value of a callback in javascript
你好,我正在做这个挑战,我无法弄清楚如何在“keys”方法中提取“each”方法的值并将这些值放入数组中写着“每个”方法不能 return 什么?更不用说我不得不制作另一个名为“eachEverything”的方法来完成第一个 objective.. 有什么方法可以完成挑战吗?
*将您的函数 'each' 声明为“_”对象上的方法。
'each' 需要两个输入:
- 一个集合(数组或对象)和
- 一个回调函数。
'each' 为每个元素运行一次回调函数
在集合中(值、key/index 和集合)。
'each' 没有 return 任何东西。
// 你的代码在这里
let _ = {
eachEverything: function(collection, callback) {
for (let coll in collection) {
callback(`${collection[coll]} ${coll} ${JSON.stringify(collection)}`);
}
},
each: function(collection, callback) {
for (let coll in collection) {
callback(coll);
}
},
keys: function(object) {
this.each(object, console.log);
}
};
// 每个测试
console.log("#### object ####");
_.each({a: 1, b: 2, c: 3}, console.log);
console.log("#### array ####");
_.each([1, 2, 3], console.log);
应该打印出来:
#### object ####
1 'a' { a: 1, b: 2, c: 3 }
2 'b' { a: 1, b: 2, c: 3 }
3 'c' { a: 1, b: 2, c: 3 }
#### array ####
1 0 [ 1, 2, 3 ]
2 1 [ 1, 2, 3 ]
3 2 [ 1, 2, 3 ]
在“_”对象上声明一个新方法 'keys'。
'keys' 应该将一个对象作为输入,return 一个数组
对象的键。
您不能使用 Object.keys 方法。
您的函数应该在其中使用 'each' 方法。
*/
// 测试
const testObject = { a: 1, b: 2, c: 3, d: 4 };
const expected = _.keys(testObject);
const actual = ["a", "b", "c", "d"];
console.log("##### these are the tests for 'keys' #####");
if (JSON.stringify(expected) === JSON.stringify(actual)) {
console.log('Yay!');
} else {
console.log('Try again!');
}
each
方法的正确实现是遍历作为参数接收的 object 的 属性 名称,然后将以下三项传递给回调函数:
- 属性
的值
- 属性 自己命名
- collection
each: function (collection, callback) {
let ownPropertyNames = Object.getOwnPropertyNames(collection);
// remove the "length" property in case of an array
if (collection instanceof Array) {
ownPropertyNames.splice(-1);
}
for (let key of ownPropertyNames) {
callback(collection[key], key, collection);
}
}
并且对于 keys
方法,您可以将回调函数传递给 each
方法,该方法仅推送 key
,作为参数接收到在 [=14 中声明的数组=]方法。
keys: function(obj) {
let keys = [];
this.each(obj, (value, key) => keys.push(key));
return keys;
}
工作演示:
let _ = {
each: function (collection, callback) {
let ownPropertyNames = Object.getOwnPropertyNames(collection);
// remove the "length" property in case of an array
if (collection instanceof Array) {
ownPropertyNames.splice(-1);
}
for (let key of ownPropertyNames) {
callback(collection[key], key, collection);
}
},
keys: function(obj) {
let keys = [];
this.each(obj, (value, key) => keys.push(key));
return keys;
}
};
console.log("==================================");
console.log("TESTS FOR 'each()' method");
console.log("==================================");
console.log("#### object ####");
_.each({a: 1, b: 2, c: 3}, console.log);
console.log("#### array ####");
_.each([1, 2, 3], console.log);
console.log("==================================");
console.log("TESTS FOR 'keys()' method");
console.log("==================================");
const testObject = { a: 1, b: 2, c: 3, d: 4 };
const expected = _.keys(testObject);
const actual = ["a", "b", "c", "d"];
if (JSON.stringify(expected) === JSON.stringify(actual)) {
console.log('Yay!');
} else {
console.log('Try again!');
}
你好,我正在做这个挑战,我无法弄清楚如何在“keys”方法中提取“each”方法的值并将这些值放入数组中写着“每个”方法不能 return 什么?更不用说我不得不制作另一个名为“eachEverything”的方法来完成第一个 objective.. 有什么方法可以完成挑战吗?
*将您的函数 'each' 声明为“_”对象上的方法。
'each' 需要两个输入:
- 一个集合(数组或对象)和
- 一个回调函数。
'each' 为每个元素运行一次回调函数 在集合中(值、key/index 和集合)。 'each' 没有 return 任何东西。
// 你的代码在这里
let _ = {
eachEverything: function(collection, callback) {
for (let coll in collection) {
callback(`${collection[coll]} ${coll} ${JSON.stringify(collection)}`);
}
},
each: function(collection, callback) {
for (let coll in collection) {
callback(coll);
}
},
keys: function(object) {
this.each(object, console.log);
}
};
// 每个测试
console.log("#### object ####");
_.each({a: 1, b: 2, c: 3}, console.log);
console.log("#### array ####");
_.each([1, 2, 3], console.log);
应该打印出来:
#### object ####
1 'a' { a: 1, b: 2, c: 3 }
2 'b' { a: 1, b: 2, c: 3 }
3 'c' { a: 1, b: 2, c: 3 }
#### array ####
1 0 [ 1, 2, 3 ]
2 1 [ 1, 2, 3 ]
3 2 [ 1, 2, 3 ]
在“_”对象上声明一个新方法 'keys'。
'keys' 应该将一个对象作为输入,return 一个数组 对象的键。
您不能使用 Object.keys 方法。
您的函数应该在其中使用 'each' 方法。 */
// 测试
const testObject = { a: 1, b: 2, c: 3, d: 4 };
const expected = _.keys(testObject);
const actual = ["a", "b", "c", "d"];
console.log("##### these are the tests for 'keys' #####");
if (JSON.stringify(expected) === JSON.stringify(actual)) {
console.log('Yay!');
} else {
console.log('Try again!');
}
each
方法的正确实现是遍历作为参数接收的 object 的 属性 名称,然后将以下三项传递给回调函数:
- 属性 的值
- 属性 自己命名
- collection
each: function (collection, callback) {
let ownPropertyNames = Object.getOwnPropertyNames(collection);
// remove the "length" property in case of an array
if (collection instanceof Array) {
ownPropertyNames.splice(-1);
}
for (let key of ownPropertyNames) {
callback(collection[key], key, collection);
}
}
并且对于 keys
方法,您可以将回调函数传递给 each
方法,该方法仅推送 key
,作为参数接收到在 [=14 中声明的数组=]方法。
keys: function(obj) {
let keys = [];
this.each(obj, (value, key) => keys.push(key));
return keys;
}
工作演示:
let _ = {
each: function (collection, callback) {
let ownPropertyNames = Object.getOwnPropertyNames(collection);
// remove the "length" property in case of an array
if (collection instanceof Array) {
ownPropertyNames.splice(-1);
}
for (let key of ownPropertyNames) {
callback(collection[key], key, collection);
}
},
keys: function(obj) {
let keys = [];
this.each(obj, (value, key) => keys.push(key));
return keys;
}
};
console.log("==================================");
console.log("TESTS FOR 'each()' method");
console.log("==================================");
console.log("#### object ####");
_.each({a: 1, b: 2, c: 3}, console.log);
console.log("#### array ####");
_.each([1, 2, 3], console.log);
console.log("==================================");
console.log("TESTS FOR 'keys()' method");
console.log("==================================");
const testObject = { a: 1, b: 2, c: 3, d: 4 };
const expected = _.keys(testObject);
const actual = ["a", "b", "c", "d"];
if (JSON.stringify(expected) === JSON.stringify(actual)) {
console.log('Yay!');
} else {
console.log('Try again!');
}