我正在尝试构建一个JS函数以将数据结构转换为
 从“开始”到“预期”的形式。

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


需要转换为-

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

最佳答案

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

08-06 06:00
查看更多