js的使用总结(一)

根据一个以知数组的code,对另一个未知数组排序.

let list = [
  {
    name: '华东',
    code: '3'
  },
  {
    name: '华南',
    code: '2'
  },
  {
    name: '华北',
    code: '1'
  },
  {
    name: '中西',
    code: '4'
  }
];
list.sort((a,b)=>{
    //目标code
    let order=['1','2','3','4'];
    return order.indexOf(a.code)-order.indexOf(b.code);
});
console.log(list);
/*
*排序后的结果
[
    {name: "华北", code: "1"},
    {name: "华南", code: "2"},
    {name: "华东", code: "3"} ,
    {name: "中西", code: "4"}
]

es5 数组对象去重

// 数组 去重
let obj = {}
let peon = listSubje.reduce((prev, cur) => {
  obj[cur.subjectId] ? '' : (obj[cur.subjectId] = true && prev.push(cur))
  return prev
}, [])

js树形递归获取树形里面的id值

let tree = []
function findDep(node = []) {
  for (let item of node) {
    tree.push(item.id)
    if (item.children) {
      findDep(item.children)
    }
  }
}
findDep([{id: 1, name: 'arr'}])
console.log(tree, '[1]')

你可能感兴趣的:(javascript,前端,开发语言)