文章目录
一、数组Array
const inventory = [
{ name: "芦笋", type: "蔬菜", quantity: 5 },
{ name: "香蕉", type: "水果", quantity: 0 },
{ name: "山羊", type: "肉", quantity: 23 },
{ name: "樱桃", type: "水果", quantity: 5 },
{ name: "鱼", type: "肉", quantity: 22 }
];
1、forEach
Array.prototype.forEachCustom = function (cb) {
if (!Array.isArray(this)) {
return;
}
if (typeof cb !== "function") {
return;
}
for (let i = 0; i < this.length; i++) {
cb(this[i], i, this);
}
};
2、filter
Array.prototype.filterCustom = function (cb) {
if (!Array.isArray(this)) {
return;
}
if (typeof cb !== "function") {
return;
}
const result = [];
for (let i = 0; i < this.length; i++) {
if (cb(this[i], i, this)) {
result.push(this[i]);
}
}
return result;
};
const resultFilterCustom = inventory.filterCustom((item) => {
return item.quantity >= 6;
});
3、map
Array.prototype.mapCustom = function (cb) {
if (!Array.isArray(this)) {
return;
}
if (typeof cb !== "function") {
return;
}
const result = [];
for (let i = 0; i < this.length; i++) {
result.push(cb(this[i], i, this));
}
return result;
};
const resultMapCustom = inventory.mapCustom((item) => {
if (item.quantity >= 6) {
return item.name;
}
});
4、reduce
Array.prototype.reduceCustom = function (cb, initialValue) {
if (!Array.isArray(this)) {
return;
}
if (typeof cb !== "function") {
return;
}
let result = initialValue;
for (let i = 0; i < this.length; i++) {
result = cb(result, this[i], i, this);
}
return result;
};
const resultReduceCustom = inventory.reduceCustom((acc, item) => {
return acc + item.quantity;
}, 0);
5、find
Array.prototype.findCustom = function (cb) {
if (!Array.isArray(this)) {
return;
}
if (typeof cb !== "function") {
return;
}
for (let i = 0; i < this.length; i++) {
if (cb(this[i], i, this)) {
return this[i];
}
}
};
const resultFindCustom = inventory.findCustom((item) => {
return item.name === "香蕉";
});
6、findIndex
Array.prototype.findIndexCustom = function (cb) {
if (!Array.isArray(this)) {
return;
}
if (typeof cb !== "function") {
return;
}
for (let i = 0; i < this.length; i++) {
if (cb(this[i], i, this)) {
return i;
}
}
};
const resultFindIndexCustom = inventory.findIndexCustom((item) => {
return item.name === "樱桃";
});
7、includes
Array.prototype.includesCustom = function (value) {
for (let i = 0; i < this.length; i++) {
if (this[i] === value) {
return true;
}
}
return false;
};
8、join
Array.prototype.joinCustom = function (separator) {
if (!Array.isArray(this)) {
return;
}
if (typeof separator !== "string") {
return;
}
let result = "";
for (let i = 0; i < this.length; i++) {
result += this[i] + separator;
}
return result.slice(0, -1);
};
console.log("joinCustom", [1, 2, 3, 4, 5].joinCustom("-"));
二、对象Object
1、Object.keys
const parentFunc = function () {
this.name = "张三";
};
const childFunc = function () {
parentFunc.call(this);
this.age = 20;
};
parentFunc.prototype.getName = function () {
return this.name;
};
childFunc.prototype = new parentFunc();
const childObj = new childFunc();
const keysAll = [];
const keysOwn = [];
for (let key in childObj) {
keysAll.push(key);
if (childObj.hasOwnProperty(key)) {
keysOwn.push(key);
}
}
console.log("Object.keysCustom", keysAll, keysOwn);
Object.prototype.keysCustom = function (obj) {
if (typeof obj !== "object" || obj === null) {
return;
}
const result = [];
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
result.push(key);
}
}
return result;
};
const obj = { name: "张三", age: 20, gender: "男" };
2、深复制
const data = [
{ name: "张三", age: 20, array: [1, 2, 3] },
{ name: "李四", age: 25, array: [4, 5, 6] }
];
const deepClone = (source) => {
if (typeof source !== "object" || source == null) {
return source;
}
const target = Array.isArray(source) ? [] : {};
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
if (typeof source[key] === "object" && source[key] !== null) {
target[key] = deepClone(source[key]);
} else {
target[key] = source[key];
}
}
}
return target;
};
console.log("deepCopy", deepClone(data));