Giter VIP home page Giter VIP logo

Comments (13)

ningt avatar ningt commented on July 21, 2024 15
function foo(o, arr) {
    return arr.reduce((acc, v) => {
	acc[v] = o[v];
	return acc;
    }, {});
}

from frontend-interview.

leecz avatar leecz commented on July 21, 2024 11
function foo (o, arr) {
  const result = {}
  Object.keys(o).filter(k => arr.includes(k)).map(s => { result[s] = o[s] })
  return result
}

from frontend-interview.

rianma avatar rianma commented on July 21, 2024 10
const foo = (obj, arr) => arr.reduce((ret, key) => Object.assign(ret, { [key]: obj[key] }), {})

不需要 return 的写法

from frontend-interview.

ucev avatar ucev commented on July 21, 2024 7

这种情况下用 forEach 更好吧

function foo(o, arr) {
  var obj = {};
  arr.forEach((i) => obj[i] = o[i]);
  return obj;
}

from frontend-interview.

huzidaha avatar huzidaha commented on July 21, 2024 7

大家都想得挺远...

const foo = (o, arr) => arr.reduce((ret, key) => {
  ret[key] = o[key]
  return ret
}, {})

from frontend-interview.

wangyue57 avatar wangyue57 commented on July 21, 2024 3
  let foo = (obj, arr) => JSON.parse(JSON.stringify(obj, arr))

from frontend-interview.

zhangolve avatar zhangolve commented on July 21, 2024

坦白讲,这道题目是在一套笔试题中的最后一道,整套题目给了我一个小时完成,后来剩最后十分钟,还剩这一道题,当时也就多少有点慌了,还有就是,纯手写的话,有些思路打不开,也被他题目里特地强调的使用ES6吓到了。

const obj={a:1,b:2,c:3};
function foo(o,arr){
	var obj={};
	arr.map((i)=>obj[i]=o[i]);
	return obj;
}

foo(obj,["a","c"]);

刚刚简单实现了一下,要说有用ES6的就是箭头函数了。不知道还有哪些更好的方法。

from frontend-interview.

zhishaofei3 avatar zhishaofei3 commented on July 21, 2024

我支持@zhangolve的版本

from frontend-interview.

 avatar commented on July 21, 2024
  • let foo=(o,arr)=>{
  • let obj={};
  • arr.forEach((i)=>obj[i]=o[i]);
  • return obj;
  • }
  • foo(obj,["a","c"]);

from frontend-interview.

mqliutie avatar mqliutie commented on July 21, 2024

reduce 方法用的还是少啊~~~,平时真的很少用

from frontend-interview.

bpup avatar bpup commented on July 21, 2024
const foo=(obj,arr)=>{

    var res=arr.reduce((res,cur)=>{
        res[cur]=obj[cur]
        return res
    },{})
    console.log(res)
    return res
}
const obj={a:1,b:2,c:3};
foo(obj,["a","c"]);

from frontend-interview.

mqliutie avatar mqliutie commented on July 21, 2024

@Wang-NQXB 你这方法会漏情况

const obj={a:1,b:2,c:function(){console.log(123)}};

这种情况你就会把c丢掉

from frontend-interview.

liyuanqiu avatar liyuanqiu commented on July 21, 2024

纯粹卖弄语法,娱乐一下:

function foo(...args) {
  const [o, arr] = args;
  return arr.reduce((acc, curr) => ({
    ...acc,
    [curr]: o[curr],
  }), {});
}

// test
foo({a:1,b:2,c:3}, ['a', 'c']);

from frontend-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.