We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
/* * 说明:请实现 flatten(input) 函数,input 为一个 javascript 对象(Object 或者 Array),返回值为扁平化后的结果。 * 示例: * var input = { * a: 1, * b: [ 1, 2, { c: true }, [ 3 ] ], * d: { e: 2, f: 3 }, * g: null, * } * var output = flatten(input); * output如下 * { * "a": 1, * "b[0]": 1, * "b[1]": 2, * "b[2].c": true, * "b[3][0]": 3, * "d.e": 2, * "d.f": 3, * // "g": null, 值为null或者undefined,丢弃 * } */
var result = {} function isObject(val) { return Object.prototype.toString.call(val) === '[object Object]' } function isArray(val) { return Array.isArray(val) } function flat(input, prefix = "") { if (isObject(input)) { for (let key in input) { let item = input[key]; if (!isObject(item) && !isArray(item) && item !== null && item !== undefined) { prefix ? result[`${prefix}.${key}`] = item : result[`${key}`] = item } else if (isObject(item) || isArray(item)) { flat(item, prefix ? `${prefix}.${key}` : key) } } } else if (isArray(input)) { for (let key in input) { let item = input[key]; if (!isObject(item) && !isArray(item) && item !== null && item !== undefined) { prefix ? result[`${prefix}[${key}]`] = item : result[`${key}`] = item } else if (isArray(item) || isObject(item)) { flat(item, prefix ? `${prefix}[${key}]` : key) } } } else if (input !== null && input !== undefined) { result[prefix] = input } }
The text was updated successfully, but these errors were encountered:
/** * 函数组合运行 * 说明:实现一个方法,可将多个函数方法按从左到右的方式组合运行。 * 如`composeFunctions(fn1,fn2,fn3,fn4)`等价于`fn4(fn3(fn2(fn1))`。 * 示例: * const add = x => x + 1; * const multiply = (x, y) => x * y; * const multiplyAdd = composeFunctions(multiply, add); * multiplyAdd(3, 4) // 返回 13 */
function composeFunctions() { var args = Array.prototype.slice.call(arguments) return function () { if (args.length === 1) { return args[0].apply(this, Array.prototype.slice.call(arguments)) } else { return composeFunctions(...args.slice(1))(args[0].apply(this, Array.prototype.slice.call(arguments))) } } } const add = x => x + 1; const multiply = (x, y) => x * y; const multiplyAdd = composeFunctions(multiply, add); console.log(multiplyAdd(3, 4)); // 返回 13
Sorry, something went wrong.
No branches or pull requests
请实现 flatten(input) 函数,input 为一个 javascript 对象(Object 或者 Array),返回值为扁平化后的结果。
The text was updated successfully, but these errors were encountered: