Skip to content
New issue

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

常见的js面试题 #29

Open
Pasoul opened this issue May 17, 2020 · 1 comment
Open

常见的js面试题 #29

Pasoul opened this issue May 17, 2020 · 1 comment

Comments

@Pasoul
Copy link
Owner

Pasoul commented May 17, 2020

请实现 flatten(input) 函数,input 为一个 javascript 对象(Object 或者 Array),返回值为扁平化后的结果。

/*
 * 说明:请实现 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
  }
}
@Pasoul Pasoul changed the title 请实现 flatten(input) 函数,input 为一个 javascript 对象(Object 或者 Array),返回值为扁平化后的结果。 js面试题 May 17, 2020
@Pasoul
Copy link
Owner Author

Pasoul commented May 17, 2020

/**
 * 函数组合运行
 * 说明:实现一个方法,可将多个函数方法按从左到右的方式组合运行。
 *   如`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

@Pasoul Pasoul changed the title js面试题 常见的js面试题 May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant