2017-09-14 2 views
0

J'essaye de construire une fonction JS pour convertir la structure de données sous la forme de 'start' à la forme de 'expected'.Javascript fonction transformer d'une structure de données à l'autre

utilisant JS méthode map(), comment pourrais-je faire pour le array- associatif suivant

const start = { 
    Clients: { 
    171: { id: 171, name: 'John Smith', active: false }, 
    172: { id: 172, name: 'Jacob Jacobson', active: true }, 
    1441: { id: 1441, name: 'Eric Ericsson', active: true }, 
    }, 
    Caregivers: { 
    1: { id: 1, name: 'John Johnson', active: true }, 
    37: { id: 37, name: 'James Jameson', active: false }, 
    15: { id: 15, name: 'Aaron Aaronson', active: true }, 
    }, 
    Doctors: { 
    1147: { id: 1147, name: 'Doc Docson', active: true }, 
    }, 
    Hospitals: { 
    115: { id: 115, active: false, name: "St. Mary's" }, 
    }, 
    Applicants: { 
    17345: { id: 17345, name: 'Bob Bobson', active: true }, 
    17346: { id: 17346, name: 'Jeff Jeffson', active: false }, 
    17347: { id: 17347, name: 'Frank Frankson', active: true }, 
    17348: { id: 17348, name: 'Bill Billson', active: true }, 
    }, 
}; 

doit être converti To-

const expected = [ 
    { label: 'Bill Billson', value: 17348, group: 'Applicants' }, 
    { label: 'Bob Bobson', value: 17345, group: 'Applicants' }, 
    { label: 'Frank Frankson', value: 17347, group: 'Applicants' }, 
    { label: 'Aaron Aaronson', value: 15, group: 'Caregivers' }, 
    { label: 'John Johnson', value: 1, group: 'Caregivers' }, 
    { label: 'Eric Ericsson', value: 1441, group: 'Clients' }, 
    { label: 'Jacob Jacobson', value: 172, group: 'Clients' }, 
    { label: 'Doc Docson', value: 1147, group: 'Doctors' }, 
]; 
+2

Tout d'abord, vous ne pouvez pas utiliser la carte avec un Objet. Deuxièmement, ce n'est pas votre armée personnelle. – lilezek

Répondre

1

.map() ne peut pas être utilisé directement sur les objets; au contraire, vous devrez utiliser Object.keys

const start = { 
 
    Clients: { 
 
     171: { id: 171, name: 'John Smith', active: false }, 
 
     172: { id: 172, name: 'Jacob Jacobson', active: true }, 
 
     1441: { id: 1441, name: 'Eric Ericsson', active: true } 
 
    }, 
 
    Caregivers: { 
 
     1: { id: 1, name: 'John Johnson', active: true }, 
 
     37: { id: 37, name: 'James Jameson', active: false }, 
 
     15: { id: 15, name: 'Aaron Aaronson', active: true } 
 
    }, 
 
    Doctors: { 
 
     1147: { id: 1147, name: 'Doc Docson', active: true } 
 
    }, 
 
    Hospitals: { 
 
     115: { id: 115, active: false, name: "St. Mary's" } 
 
    }, 
 
    Applicants: { 
 
     17345: { id: 17345, name: 'Bob Bobson', active: true }, 
 
     17346: { id: 17346, name: 'Jeff Jeffson', active: false }, 
 
     17347: { id: 17347, name: 'Frank Frankson', active: true }, 
 
     17348: { id: 17348, name: 'Bill Billson', active: true } 
 
    } 
 
}; 
 

 
// Get an array of properties in 'start' 
 
// then use Array.reduce() to loop over each item 
 
const expected = Object.keys(start).reduce((res, gKey) => { 
 

 
    // gKey = 'group' name 
 
    // gVal = 'group' value 
 
    let gVal = start[gKey]; 
 
    
 
    // loop over each item in the 'group' 
 
    Object.keys(gVal).forEach(iKey => { 
 
    
 
     // iKey = 'group.item' name 
 
     // iVal = 'group.item' value 
 
     let iVal = gVal[iKey]; 
 
     
 
     // if the value's .active property is truthy 
 
     if (iVal.active) { 
 
     
 
      // format the result as desired and add it to the result array 
 
      res.push({ 
 
       label: iVal.name, 
 
       value: iKey, 
 
       group: gKey 
 
      }); 
 
     } 
 
    }); 
 
    
 
    // return the result array 
 
    return res; 
 
    
 
// start the .reduce() with an empty array 
 
}, []); 
 
console.log(expected);

0

à boucle sur un objet, vous pouvez soit utiliser a for ... in loop, ou utilisez Object.keys pour obtenir un tableau de clés. Car ... inclura les propriétés héritées, vous devrez donc les filtrer manuellement. Object.keys seulement des rendements propres propriétés, donc il n'y a pas besoin de faire le filtrage (mais il est pas approprié si vous avez besoin des propriétés héritées du passé)

Exemple avec ... dans:

for (var prop in start) { 
    if (start.hasOwnProperty(prop)) { 
     // logs out 'Clients', then 'Caregivers', then 'Doctors', then 'Hospitals', then 'Applicants' 
     console.log(prop); 
    } 
} 

Exemple avec Object.keys:

//produces array ['Clients', 'Caregivers', 'Doctors', 'Hospitals', 'Applicants'] 
var keys = Object.keys(start); 

donc, si vous vouliez utiliser .map, vous pouvez commencer par cela, et le remplir pour faire ce que vous désirez:

Object.keys(start) 
    .map(key => { 
     //do something with start[key] 
     //perhaps you could get Object.keys(start[key]) and loop over that as well. 
    }); 
0

Ma solution sans 'foreach':

function transform(data) { 
 
    Object.entries(data).map(item => Object.values(item[1]) 
 
    .map(i => i.group = item[0])) 
 
    .reduce((acc, cur) => acc.concat(cur), []) 
 
    .filter(item => item.active === true) 
 
    .sort((a, b) => a.group - b.group) 
 
    .map(item => { 
 
     let expected = {}; 
 
     expected.label = item.name; 
 
     expected.value = item.id; 
 
     expected.group = item.group; 
 
    }); 
 
    
 
    return expected; 
 
}