跳到主要内容

mapKeys

2024年06月17日
柏拉文
越努力,越幸运

一、认识


二、语法


三、实现


function mapKeys(object, iteratee) {
const objectCopy = Object(object);
const result = {};
Object.keys(objectCopy).forEach((key) => {
const value = objectCopy[key];
result[iteratee(value, key, objectCopy)] = value;
});
return result;
}

const result = mapKeys({ a: 1, b: 2 }, function (value, key) {
return key + value;
});
console.log(result);