2017-09-14 84 views
0

我试图构建一个JS函数,将数据结构以 'start'的形式转换为'expected'形式。Javascript函数 - 从一个数据结构到另一个数据结构的转换

使用JS map()方法,我会怎么做以下联想基于阵列的需求

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

了一个对象,您可以使用转换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

首先,你不能使用地图的目的。其次,这不是你的私人军队。 – lilezek

回答

1

.map()不能直接在对象上使用;相反,你需要使用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

要循环一个for ... in循环,或者使用Object.keys来获取一组键。 For ... in将包含继承的属性,因此您可能需要手动将其过滤掉。 Object.keys只返回自己的属性,所以没有必要做过滤(但如果你需要继承属性也不宜)

实例与...在:

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

例与Object.keys:

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

所以,如果你想使用.map,你可以用这个启动,并填写它做任何你想要的:

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

我的解决方案,而 '的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; 
 
}

相关问题