1. const arr = [{label: '男', value: 0},{label: '女', value: 1}]
      2. function f(arr) {
      3. // 写代码
      4. // reduce
      5. }
      6. const obj = f(arr) // obj ===> {0: '男', 1:'女'}
      答案:
      1. const arr = [{label: '男', value: 0},{label: '女', value: 1}]
      2. function f(arr) {
      3. const obj = {}
      4. arr.forEach(item => { obj[item.value] = item.label})
      5. return obj
      6. }
      7. const obj = f(arr)
      8. console.log(obj)
      用数组方法 reduce() 方法 ```jsx const arr = [{label: ‘男’, value: 0},{label: ‘女’, value: 1}] function f(arr){ const obj = arr.reduce((acc,item) => { acc[item.value] = item.label return acc },{}) return obj } const obj = f(arr) console.log(obj)

    //优化写法 function f(arr){ const obj = arr.reduce((acc,item) => ({…acc,[item.value]:item.label}),{}) return obj } const obj = f(arr) console.log(obj)

    1. 2.
    2. ```jsx
    3. const obj = { 0: '男', 1:'女'}
    4. function f(obj) {
    5. // 写代码
    6. }
    7. const arr = f(obj) // arr ===> [{label: '男', value: 0},{label: '女', value: 1}]

    答案

    1. const obj = { 0: '男', 1:'女'}
    2. const arr = []
    3. Object.keys(obj).forEach(item => {
    4. arr.push({label:obj[item],value:item})
    5. })
    6. console.log(arr)
    1. map方法
    1. const obj = { 0: '男', 1:'女'}
    2. const arr = Object.keys(obj).map(item =>{
    3. return {
    4. lable: obj[item],
    5. value: item
    6. }
    7. })
    8. console.log(arr)
    9. //优化写法
    10. // 使用箭头函数省略return 必须用小括号包上,否则箭头函数会以为内容是一个函数
    11. const arr = Object.keys(obj).map(item => ({
    12. lable: obj[item],
    13. value: item
    14. }))
    15. console.log(arr)
    1. ```jsx var data = [ {id:”01”, name: “张大大”, pid:””, job: “项目经理”}, {id:”02”, name: “小亮”, pid:”01”, job: “产品leader”}, {id:”03”, name: “小美”, pid:”01”, job: “UIleader”}, {id:”04”, name: “老马”, pid:”01”, job: “技术leader”}, {id:”05”, name: “老王”, pid:”01”, job: “测试leader”}, {id:”06”, name: “老李”, pid:”01”, job: “运维leader”}, {id:”07”, name: “小丽”, pid:”02”, job: “产品经理”}, {id:”08”, name: “大光”, pid:”02”, job: “产品经理”}, {id:”09”, name: “小高”, pid:”03”, job: “UI设计师”}, {id:”10”, name: “小刘”, pid:”04”, job: “前端工程师”}, {id:”11”, name: “小华”, pid:”04”, job: “后端工程师”}, {id:”12”, name: “小李”, pid:”04”, job: “后端工程师”}, {id:”13”, name: “小赵”, pid:”05”, job: “测试工程师”}, {id:”14”, name: “小强”, pid:”05”, job: “测试工程师”}, {id:”15”, name: “小涛”, pid:”06”, job: “运维工程师”} ]

    // 问题1. 找出 与 小刘 处于统一领导下的同事 function f(data, name){ // 完成代码 } const arr = f(data, ‘小刘’) console.log(arr) // // [{id:”10”, name: “小刘”, pid:”04”, job: “前端工程师”}, // {id:”11”, name: “小华”, pid:”04”, job: “后端工程师”}, // {id:”12”, name: “小李”, pid:”04”, job: “后端工程师”}]

    // 问题2. 找出 小亮 的所有下属 function f2(data, name){ // 完成代码

    } const arr = f2(data, ‘小亮’) console.log(arr) // // [{id:”07”, name: “小丽”, pid:”02”, job: “产品经理”}, // {id:”08”, name: “大光”, pid:”02”, job: “产品经理”}]

    1. 问题一
    2. ```jsx
    3. // 找出与 小刘 处于统一领导下的同事
    4. function f(data, name){
    5. //获取小刘的pid
    6. const arr = data.find(item => item.name === name).id
    7. // 根据小刘的pid值来判断获取他的同事
    8. return data.filter(item=> item.pid === arr)
    9. }
    10. const arr = f(data, '小刘')
    11. console.log(arr) //
    12. // [{id:"10", name: "小刘", pid:"04", job: "前端工程师"},
    13. // {id:"11", name: "小华", pid:"04", job: "后端工程师"},
    14. // {id:"12", name: "小李", pid:"04", job: "后端工程师"}]
    1. 问题二
    1. function f(data, name) {
    2. return data.filter(
    3. item => item.pid === data.find(item => item.name === name).id
    4. )
    5. }
    6. const arr = f(data, '小亮')
    7. console.log(arr)
    8. // [{id:"07", name: "小丽", pid:"02", job: "产品经理"},
    9. // {id:"08", name: "大光", pid:"02", job: "产品经理"}]

    数组转树

    1. const arr = []
    2. // 1.对data第一轮循环:
    3. // 1)给每一次list中的对象补充 children属性,直接赋值空数组
    4. // 2)建立一个字典(方便后期做查询),{属性名是id:值就是对象}
    5. const map = {}
    6. data.forEach(item => {
    7. item.children = []
    8. map[item.id] = item
    9. })
    10. // 2.对data第二轮循环
    11. // 根据 pid 找父子关系
    12. data.forEach(item => {
    13. // 对于当前的元素,如果它的pid对应的元素存在,说明它是下属,进入 children
    14. // 对于当前的元素,如果它的pid对应的元素不存在,说明它不是下属,是顶级元素,进入 arr
    15. const father = map[item.pid]
    16. if (father) {
    17. father.children.push(item) // item是father 下级
    18. } else { // 找不到上级,说明item就是顶级元素
    19. arr.push(item)
    20. }
    21. })
    22. return arr
    1. ```jsx var obj = {label:’男’,value: 1,age: 18}

    function f(obj) { // 写代码,得到 } var arr = f(obj); console.log(arr) // arr = [‘男’, ‘1’, ‘18’] // 所有的属性值取出来,保存在数组中

    1. 答案
    2. ```jsx
    3. var obj = {label:'男',value: 1,age: 18}
    4. var arr = []
    5. for (let i in obj) {
    6. arr.push(obj[i]);
    7. }
    8. console.log(arr);