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

将数组转化成自定义hash键的对象 #59

Open
youngwind opened this issue Apr 9, 2016 · 0 comments
Open

将数组转化成自定义hash键的对象 #59

youngwind opened this issue Apr 9, 2016 · 0 comments
Labels

Comments

@youngwind
Copy link
Owner

要解决的问题

平常在处理数据的时候我们常常需要进行类似这样的处理。

var before = [
{
  id:1,
  name:"youngwind',
  age:24
},
{
 id:2,
 name:"xiaoye",
 age:30
}
];

把数组的每一项中的id拿出来作为索引值,重新构建成一个对象,这样以后我每次想查找特定id的名字的时候就不用遍历数组了。

{
  1:{
    name:"youngwind",
    age:24
  },
  2:{
    name:"xiaoye",
    age:30
  }
}

就是这么简单的一个功能,我找了好多工具类库都没找着,只好自己写了一个。
https://www.npmjs.com/package/array-to-hash-object

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

No branches or pull requests

1 participant