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

实现一个 setter 方法 #386

Open
lgwebdream opened this issue Jul 6, 2020 · 4 comments
Open

实现一个 setter 方法 #386

lgwebdream opened this issue Jul 6, 2020 · 4 comments
Labels
JavaScript teach_tag 编程题 teach_tag

Comments

@lgwebdream
Copy link
Owner

let setter = function (conten, key, value) {
  // your code
};
let n = {
  a: {
    b: {
      c: { d: 1 },
      bx: { y: 1 },
    },
    ax: { y: 1 },
  },
};
// 修改值
setter(n, "a.b.c.d", 3);
console.log(n.a.b.c.d); //3
setter(n, "a.b.bx", 1);
console.log(n.b.bx); //1
@lgwebdream lgwebdream added JavaScript teach_tag 编程题 teach_tag labels Jul 6, 2020
@lgwebdream
Copy link
Owner Author

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

@523451928
Copy link

let n = {
  a: {
    b: {
      c: { d: 1 },
      bx: { y: 1 },
    },
    ax: { y: 1 },
  },
}
function flatObj(obj, parentKey = '', res = {}) {
  for (const key in obj) {
    const keyName = `${parentKey}${key}`
    if (typeof obj[key] === 'object') {
      flatObj(obj[key], `${keyName}.`, res)
    } else {
      res[keyName] = obj[key]
    }
  }
  return res
}
function deFlatObj(obj) {
  const res = {}
  for (const key in obj) {
    key.split('.').reduce((acc, item, index, origin) => {
      acc[item] = acc[item] || (!origin[index + 1] && obj[key]) || {}
      return acc[item]
    }, res)
  }
  return res
}

function setter(conten, key, value) {
  const obj = flatObj(conten)
  obj[key] = value
  return deFlatObj(obj)
}
setter(n, 'a.b.c.d', 3)

@GolderBrother
Copy link

const setter = function (content, key, value) {
  if (!content || typeof content !== "object") return false;
  const paths = key.split(".") || [];
  // 根据paths, 不停的获取最后对应的数据和key
  const { obj, keyName } = paths.reduce((accu, k, index) => {
    if (index !== paths.length - 1) return accu[k];
    return { obj: accu, keyName: k };
  }, content);
  obj[keyName] = value;
  return obj;
};
let n = {
  a: {
    b: {
      c: { d: 1 },
      bx: { y: 1 },
    },
    ax: { y: 1 },
  },
};

// 修改值
setter(n, "a.b.c.d", 3);
console.log(n.a.b.c.d); //3
setter(n, "a.b.bx", 1);
console.log(n.a.b.bx); //1
console.log(n);

@zizxzy
Copy link

zizxzy commented Nov 4, 2021

const setter = (obj, keyStr, value) => {
  if (typeof obj !== 'object' || !obj) return false;
  const pathArr = keyStr.split('.') || [];
  const { content, key } = pathArr.reduce((accumulator, currentValue, currentIndex) => {
    if (currentIndex !== pathArr.length - 1) return accumulator[currentValue];
    return { 'content': accumulator, 'key': currentValue };
  }, obj);
  content[key] = value;
  return true;
}

let n = {
  a: {
    b: {
      c: { d: 1 },
      bx: { y: 1 },
    },
    ax: { y: 1 },
  },
};

// 修改值
setter(n, "a.b.c.d", 3);
console.log(n.a.b.c.d); //3
setter(n, "a.b.bx", 1);
console.log(n.a.b.bx); //1
console.log(n);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 编程题 teach_tag
Projects
None yet
Development

No branches or pull requests

4 participants