【问题标题】:Transform Nested Object Data Structure into an Array of Objects- JavaScript将嵌套对象数据结构转换为对象数组 - JavaScript
【发布时间】:2018-03-10 20:29:40
【问题描述】:

我在处理一个对象并将其重新格式化为新的数据结构时遇到了麻烦。我需要获取开始对象并执行以下操作:首先按组排序,然后标记并排除“活动:假”记录。

var beginning = {
    Sister: {
      1: { id: 1, name: 'Jesse Steven', active: false },
      2: { id: 2, name: 'Zena Wong', active: true },
      3: { id: 3, name: 'Katie Johnson', active: true },
    },
    Brother: {
      10: { id: 10, name: 'Jeff Jacobs', active: true },
      11: { id: 11, name: 'Mark Matha', active: false },
      12: { id: 12, name: 'Kyle Ford', active: true },
    },
    Friend: {
      20: { id: 20, name: 'Jim Dobbs', active: true },
    }
};

之后,应该是这样的:

var final = [
    { label: 'Jeff Jacobs', value: 10, group: 'Brother' },
    { label: 'Kyle Ford', value: 12, group: 'Brother' },
    { label: 'Jim Dobbs', value: 20, group: 'Friend' },
    { label: 'Katie Johnson', value: 3, group: 'Sister' },
    { label: 'Zena Wong', value: 2, group: 'Sister' }   
];

【问题讨论】:

    标签: javascript arrays object data-structures


    【解决方案1】:

    像这样? 它仍然缺少一种,但可以很容易地补救。

    let beginning = {
      Sister: {
        1: { id: 1, name: 'Jesse Steven', active: false },
        2: { id: 2, name: 'Zena Wong', active: true },
        3: { id: 3, name: 'Katie Johnson', active: true },
      },
      Brother: {
        10: { id: 10, name: 'Jeff Jacobs', active: true },
        11: { id: 11, name: 'Mark Matha', active: false },
        12: { id: 12, name: 'Kyle Ford', active: true },
      },
      Friend: {
        20: { id: 20, name: 'Jim Dobbs', active: true },
      }
    };
    
    let relations = Object.keys(beginning)
    let final = relations.map(function(relation){
        let num_keys = Object.keys(beginning[relation])
        return num_keys.map(function(num_key){
            beginning[relation][num_key]["group"] = relation
            return beginning[relation][num_key]
        }) 
      })
      .reduce(function(a, b){//flattens the returned array of arrays
        return a.concat(b);
      })
      .filter(function(a){//filters out only active
        return a["active"]
      })
      .map(function(a){//clean up some data
        return {
          label: a["name"],
          value: a["id"],
          group: a["group"]
        }
      })
    
    console.log(final)
    

    【讨论】:

      【解决方案2】:

      编辑:添加排序作为最初的要求。

      您可以通过多种方式执行此操作,包括 for / in 循环或 ES2015 中的一些花哨的东西,但一个相对简单的功能示例解决方案如下:

      var activePeople = Object.keys(beginning).map(person => {
        return Object.keys(beginning[person]).map(num => {
          return (!!beginning[person][num].active) ? {
            label: beginning[person][num].name,
            value: beginning[person][num].id,
            group: person
          } : null
        }).filter(i => !!i)
      })
      // flatten nested arrays
      var final = [].concat.apply([], activePeople).sort((p1, p2) => {
        if (p1.group < p2.group) {
          return -1
        } else if (p1.group > p2.group) {
          return 1
        }
      
        if (p1.label < p2.label) {
          return -1
        }
        return 1
      })
      

      【讨论】:

        【解决方案3】:

        我可以提出一个更快的代码:

        "use strict";
        
        let beginning = {
            Sister: {
                1: { id: 1, name: 'Jesse Steven', active: false },
                2: { id: 2, name: 'Zena Wong', active: true },
                3: { id: 3, name: 'Katie Johnson', active: true },
            },
            Brother: {
                10: { id: 10, name: 'Jeff Jacobs', active: true },
                11: { id: 11, name: 'Mark Matha', active: false },
                12: { id: 12, name: 'Kyle Ford', active: true },
            },
            Friend: {
                20: { id: 20, name: 'Jim Dobbs', active: true },
            }
        };
        
        let groups = Object.keys(beginning).sort();
        let final = [];
        
        for (let i = 0, max = groups.length; i < max; i++) {
            let keys = Object.keys(beginning[groups[i]]);
        
            for (let j = 0, max2 = keys.length; j < max2; j++) {
                let item = beginning[groups[i]][keys[j]];
        
                if (item['active'] ) {
                    final.push({
                        label: item['name'],
                        value: keys[j],
                        group: groups[i]
                    });
                }
        
            }
        }
        
        console.log(final);

        【讨论】:

        • 谢谢 - 这个解决方案对我来说是最容易理解的 - 然后你将如何按人名对类别进行排序?
        • 我使用 sort() 函数按 A-Z 顺序对字符串进行排序。就是这样:)
        • 排序适用于类别,但标签也应该从该类别中的 A-Z 排序。试图找出这个 .sort 应该去哪里。
        猜你喜欢
        • 2013-03-08
        • 1970-01-01
        • 2022-12-18
        • 1970-01-01
        • 2018-01-24
        • 1970-01-01
        • 2022-12-18
        • 1970-01-01
        • 2018-01-04
        相关资源
        最近更新 更多