将嵌套对象数据结构转换为对象数组 - JavaScript

时间:2017-09-29 00:57:09

标签: javascript arrays object data-structures

我在解决此问题时遇到问题,将对象重新格式化为新的数据结构。我需要获取起始对象并执行以下操作:首先按组排序,然后标记并排除“active:false”记录。

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' }   
];

3 个答案:

答案 0 :(得分:0)

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

你可以通过多种方式实现这一点,包括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
})

答案 1 :(得分:0)

喜欢这个? 它仍然缺少一种,但这很容易弥补。

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 :(得分:0)

我可以提出更快的代码:

&#13;
&#13;
"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);
&#13;
&#13;
&#13;