practice's People
practice's Issues
给定对象obj和字符串key,写一个函数get,返回obj里面key对应的值,key可有多级,用.分隔
-- 1 --
给定对象obj和字符串key,写一个函数get,返回obj里面key对应的值,key可有多级,用.分隔
如
obj = { a: { b: { c: 1 } } }, key = 'a.b.c'
get(obj, key) 得到1
obj={a:{b:{c:1}}},
key='a.b.c';
function get(obj, key) {
let args = key.split('.');
args.reduce((preValue, curValue, index, array) = >{
if (obj.hasOwnProperty(curValue)) {
obj = obj[curValue];
} else {
obj = 'obj不存在对应key';
}
return obj;
},
obj) return obj;
}
get(obj, key);
obj={a:{b:{c:1}}},
key='a.b.c';
function get(obj, key) {
let args = key.split('.');
args.forEach(v = >{
if (obj.hasOwnProperty(v)) {
obj = obj[v];
} else {
console.log('obj内不存在对应key');
}
}) return obj;
}
get(obj, key);
多层的数组 array(数组) (嵌套可以是任何层数)转换为只有一层的数组
实现一个flatten函数,将一个嵌套多层的数组 array(数组) (嵌套可以是任何层数)转换为只有一层的数组,数组中元素仅基本类型的元素或数组,不存在循环引用的情况。 Ex: flatten([1, [2], [3, [[4]]]]) => [1, 2, 3, 4];
function flatten(array) {
let newArray = [];
function resolveArr(arr) {
if (Array.isArray(arr)) {
arr.forEach(value = >{
if (Array.isArray(value)) {
resolveArr(value);
} else {
newArray.push(value);
}
});
} else {
newArray.push(arr);
}
}
resolveArr(array);
return newArray;
}
flatten([1, [2], [3, [[4]]]]);
给定整数n和m,写一个函数dispatch,把1-n尽量平均地分成m个组
-- 2 --
给定整数n和m,写一个函数dispatch,把1-n尽量平均地分成m个组
如
n = 2, m = 2
dispatch(n, m) 得到[[1], [2]]
n = 6,
m = 2;
function dispatch(n, m) {
arr = Array(m).fill('naive').map((v, i) = >{
return [];
});
subArr = Array(n).fill('naive').map((v, i) = >{
return i + 1;
});
let remainder = n % m;
let quotient = (n - remainder) / m;
let length = 0;
arr.forEach((value, index) = >{
for (i = 1; i <= quotient; i++) {
value.push(i + index * quotient);
length = i + index * quotient;
}
});
restArr = subArr.slice(length);
restArr.forEach((value, index) = >{
arr[index].push(value);
}) console.log(arr);
}
dispatch(n, m);
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.