---恢复内容开始---
1.给定一个整数数组 nums
和一个目标值 target
,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
var twoSum = function(nums, target){ let res = new Map() for(var i=0,len=nums.length;i<len;i++){ if(res.has(target - nums[i])) return [res.get(target-nums[i]), i] res.set(nums[i], i) } console.log(res) } twoSum([2,5,7,8,9], 15) (2) [2, 3]
2.递归案例:求一个数字,各个位数上的数字 和: 123 ---> 6
function getEverySum(x){ if(x<10) return x return x % 10 + getEverySum(parseInt(x/10)) } console.log(getEverySum(564564))
3. 数组 var arr = ['1-2', '2-8', '1-3', '2-4', '6-10', '2-5']
转换成:[ {key: 1, values: [2 , 3]}, {key: 2, values: [8, 4, 5]}, {key: 6, values: [10]}]
var arr = ['1-2', '2-8', '1-3', '2-4', '6-10', '2-5'] var fn = arr => { var res = [] var obj = arr.reduce((pre, cur)=> { var [k, v] = cur.split('-') pre[k] ? pre[k].push(v): pre[k] = [v] return pre }, {}) for(const [key, values] of Object.entries(obj)){ res.push({key, values}) }
// arr.map(v => v.split('-')).reduce((pre, [k, v])=> {
(pre[k] = pre[k] || []).push(v)
return pre
}, {})
// return Object.entries(obj).map(([key, values])=> ({key, values})) return res } console.log(fn(arr))
4.实现一个sum方法使其有以下表现
sum(2,3).valueOf() //5 sum(2,3,4).valueOf() //9 sum(2,3)(4).valueOf() //9 sum(2,3,4)(2)(3,4).valueOf() //18
// sum() 的结果可以继续使用 () 运算符,说明 sum() 返回的是一个函数(函数/函数表达式/箭头函数的统称);另外,这个返回值还有 valueOf() 方法,所以可以先搭个框 function sum() { const fn = () => {}; fn.valueOf = () => {}; return fn; } function sum(...args) { const r = args.reduce((a, b) => a + b); const fn = (...args) => { return sum(r, ...args); }; fn.valueOf = () => r; return fn; }
------------------------------------------------------------------------------
// 类似 fn(1).value = 1 fn(1)(2).value = 5 fn(1)(2)(3).value = 14
function fn(){
const a = function(m){
a.value = m*m
return a
}
a.value = 1
return a
}
5.递归修改json的key值
var dataObject = { "1": { "name": "第一级1", "type": "第一级2", "describe": "第一级3", "testNum": "第一级4", "1-1": { "name": "第二级5", "type": "第二级6", "describe": "第二级7", "testNum": "第二级8", "1-1-1": { "name": "第三级9", "type": "第三级q", "describe": "第三级w", "testNum": "第三级e" } } } }; // 将里面的1-1,1-1-1,1-2之类的值修改成对应的name字段里面的值,需要替换成下面这种的: var dataObject = { "第一级1": { "name": "第一级1", "type": "第一级2", "describe": "第一级3", "testNum": "第一级4", "第二级5": { "name": "第二级5", "type": "第二级6", "describe": "第二级7", "testNum": "第二级8", "第三级9": { "name": "第三级9", "type": "第三级q", "describe": "第三级w", "testNum": "第三级e" }, "第三级r": { "name": "第三级r", "type": "第三级ty", "describe": "第三级y", "testNum": "第三级y" } } } }
function generate(obj) { let _obj = {} Object.entries(obj).forEach(([key, value]) => { if (typeof value !== 'object') { _obj[key] = value } else { _obj[value.name] = generate(value) } }) return _obj } var res = generate(dataObject) console.log(res)
6. 输入一个值获取最近的值,并返回
var arr = [ ["IP-1", 0], ["IP-2", 13], ["IP-3", 68], ["IP-4", 138], ["IP-5", 149], ["IP-6", 834], ["IP-7", 1090], ["IP-8", 1247], ["IP-9", 1310], ["IP-10", 1956], ["IP-11", 2270], ["IP-12", 3337], ] // 出入 200,返回 IP-7
var res = function (value) { // 设置一个 map const map = new Map() // 设置每个 绝对值 和 name arr.forEach(x => { const number = Math.abs(value - x[1]) map.set(number, x[0]) }) // 得到 map里面最小的 value,得到名字 return map.get(Math.min.apply(null, Array.from(map.keys()))) } console.log(res(2000)) // IP-10
7. 2个多维数组查找交集 打标识
var A = [ {id: 1}, { id: 2, children: [ {id: 3}, {id: 4}, { id: 5, children: [{ id: 6 }] } ] } ]; var B = [ {id: 1}, { id: 2, children: [{ id: 5, children: [{ id: 6 }] }] }]; let c = method(A, B) //结果 c = [ {id: 1, disabled: true}, { id: 2, disabled: true, children: [ {id: 3}, {id: 4}, { id: 5, disabled: true, children: [{ id: 6, disabled: true }] } ] } ];
function method (treeA, treeB) { return dfs(treeA, new Set(flat(treeB))) } function dfs (root, dict) { for (node of root) { if (dict.has(node.id)) { node.disabled = true } if (node.children) { dfs(node.children, dict) } } return root } function flat (root, path = []) { for (node of root) { path.push(node.id) if (node.children) { flat(node.children, path) } } return path }