Skip to content

Instantly share code, notes, and snippets.

@hsuanxyz
Created May 31, 2017 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsuanxyz/39b68ab0a4282aaf3a56504e5b32ca33 to your computer and use it in GitHub Desktop.
Save hsuanxyz/39b68ab0a4282aaf3a56504e5b32ca33 to your computer and use it in GitHub Desktop.
code-view
let items = [
{id:4, config:null},
{id:4, config:null},
{id:1, config:'hello'},
{id:2, config:'hello'},
{id:7, config:null},
{id:3, config:'hello'},
]
// 定义安权重值排序的数组
let sortIndex = [1, 3, 2, 4, 5, 7];
// 去除 config 为 null 的项
let sortItems = items.filter( e => e.config !== null);
// 根据权重索引排序
sortItems.sort( (a,b) => sortIndex.indexOf(a.id) - sortIndex.indexOf(b.id));
// 第一个就是符合条件的项
console.log(sortItems[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment