const arr = [1, 2, 3, 4, 5, 99]
arr.reduce((pre, next) => pre + next) // 114
const fn = () => { console.log(11) }const obj = {}const arr = [2, '2', fn, obj]const selfIncludes = (arr, value) => {// 使用try catche的原因是快速跳出循环。try {return arr.reduce((pre, next) => {if (next === value) throw truereturn false}, false)} catch (end) {if (typeof end === 'boolean') return true;}}
const fn = () => { console.log(11) }const obj = {}const arr = [2, '2', fn, obj]const selfIndexOf = (arr, value) => {// 使用try catche的原因是快速跳出循环。try {return arr.reduce((pre, next) => {if (next === value) throw prereturn ++pre}, 0) && -1} catch (end) {if (typeof end === 'number') return end;}}console.log(selfIndexOf(arr, fn))
const fn = () => { console.log(11) }const obj = {}const arr = [2, '2', fn, obj]const selfMap = (arr, fn) => {return arr.reduce((pre, next) => {// map方法并没有这一步// pre.push(fn?.(next, pre.length) || next)pre.push(fn(next, pre.length))return pre }, [])}console.log(selfMap(arr, (item, i) => {return {name: item,index: i}}))console.log(selfMap(arr, () => {}))console.log(arr.map(() => {}))

const filterParam = (obj) => Object.entries(obj).reduce((pre, [key, value]) => {// 这里添加你需要做的判断依据if (value || value === 0) pre[key] = valuereturn pre}, {});console.log(filterParam({name: undefined, age: 0, class: 'ant-design', test: NaN}))// {age: 0, class: 'ant-design'}
const arr = [{name: '张三',age: 16},{name: '李四',age: 17},{name: '王五',age: 18},]const keyBy = (array, key) => {return array.reduce((pre, next) => {pre[next[key]] = next;return pre;}, {})}console.log(keyBy(arr, 'name'))
class Person {// 技能对象skill = {中文: (string) => {// ..... 此处经过一系列的翻译(比如说调用百度的翻译)return string;}}constructor(params) {// 初始化信息this.info = params;}// 学习技能的功能函数studySkill(skillName, skillFn) {if (this.skill[skillName]) return '这个技能我会!';this.skill[skillName] = skillFn;return `${skillName}学习完成`;}// 说话的功能函数say(string, way) {if (!this.info.isLanguage?.includes(way)) return '你确定这是一门语言吗?';if (!this.skill[way]) return '我不会这个语言!';return this.skill[way](string);}};const zs = new Person({name: 'zs',age: 18,isLanguage: ['中文', '英语', '法语', '俄语']});// 可学习的语言技能数组const skillArr = {英语: (string) => {// ..... 此处经过一系列的翻译(比如说调用百度的翻译)const end = 'Hello world';return end;},法语: (string) => {// ..... 此处经过一系列的翻译(比如说调用百度的翻译)const end = 'Bonjour, le monde.';return end;},中文: (string) => {// ..... 此处经过一系列的翻译(比如说调用百度的翻译)const end = '你好,世界。';return end;}}// reduce 主题Object.entries(skillArr).reduce((pre, next) => {const [skillName, skillFn] = next;const sayMessage = pre.studySkill(skillName, skillFn);console.log(sayMessage);return pre;}, zs);// --------------console.log(zs);console.log(zs.say('你好', '英语')) // Hello worldconsole.log(zs.say('你好', '法语')) // Bonjour, le monde.console.log(zs.say('你好', '中文')) // 你好console.log(zs.say('你好', '俄语')) // 我不会这个语言!console.log(zs.say('你好', '鸟语')) // 你确定这是一门语言吗?

上一篇:《我们仨》的句子加赏析
下一篇:把赞助商加到台词了怎样说