简体   繁体   中英

Transform Nested Object Data Structure into an Array of Objects- JavaScript

I am having trouble with this problem taking an object and reformatting it to a new data structure. I need to take the beginning object and do the following: sort by group first, then label and exclude "active: false" records.

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 },
    }
};

After, it should looks like this:

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

EDIT: Added sorting as the initial requirements asked for.

You can do this a number of ways, including for / in loops or some fancy stuff with ES2015, but a relatively simple functional example solution would be the following:

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
})

Like this? It's still missing a sort, but that can be easily remedied.

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)

I can propose faster one code:

 "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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM