Destructure an array parameter [duplicate]

限于喜欢 提交于 2021-02-04 21:17:31

问题


Is it possible to destructure a function parameter?

For example, I would want to convert this:

Object.entries({}).find(group=> group[0] === "foo" && group[1] === "bar");

To something like this:

Object.entries({}).find([0: key, 1: value] => key === "foo" && value === "bar");

or

Object.entries({}).find([...key, value] => key === "foo" && value === "bar");

回答1:


Notice that to destruct the arrow function parameters you should do ([key, value])

Code:

const obj = {
  asdf: 'asdf',
  foo: 'bar'
};

const result = Object
  .entries(obj)
  .find(([key, value]) => key === 'foo' && value === 'bar');
  
console.log(result);


来源:https://stackoverflow.com/questions/57198633/destructure-an-array-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!