-1

javaScript ES6.map()返回期望包括位置城市基于哪个activeMember: truememberDetail

const data = [
  {
    id: 131131,
    title: "Completed members",
    memberDetail: {
      "0": {
        activeMember: false,
        memberDetails: {
          location: "Mumbai",
          Country: "India"
        }
      },
      "1": {
        activeMember: true,
        memberDetails: {
          location: "London",
          Country: "England"
        }
      },
      "2": {
        activeMember: false,
        memberDetails: {
          location: "Colombo",
          Country: "Sri Lanka"
        }
      }
    }
  },
  {
    id: 130500,
    title: "Acting members",
    memberDetail: {
      "0": {
        activeMember: false,
        memberDetails: {
          location: "Paris",
          Country: "France"
        }
      },
      "1": {
        activeMember: false,
        memberDetails: {
          location: "Katmandu",
          Country: "Nepal"
        }
      },
      "2": {
        activeMember: true,
        memberDetails: {
          location: "Dublin",
          Country: "Ireland"
        }
      }
    }
  },
  {
    id: 131096,
    title: "Pending members",
    memberDetail: {
      "0": {
        activeMember: true,
        memberDetails: {
          location: "Perth",
          Country: "Australia"
        }
      },
      "1": {
        activeMember: false,
        memberDetails: {
          location: "Berlin",
          Country: "Germany"
        }
      }
    }
  }
];

const resultValue = data.map(detail => {
  var obj = {
    "id": detail.id,
    "title": detail.title,
  };
  return obj
});

console.log(resultValue);

**期待结果** 在此处输入图像描述

4

2 回答 2

2
data.map(detail => {
  var members = Object.values(detail.memberDetail);
  var activeMember = members.find(member => member.activeMember) || {};
  var memberData = activeMember.memberDetails || {};
  return {
    "id": detail.id,
    "title": detail.title,
    "location": memberData.location,
    "Country": memberData.Country
  };
})

应该这样做。

如果没有活跃会员location,则Country返回undefined。如果您希望他们在这种情况下返回一个空字符串,只需编写

...
"location": memberData.location || "",
"Country": memberData.Country || ""
...

如果有多个活动成员,它会选择具有最低 id 的成员。

于 2017-07-26T22:44:40.823 回答
0

您可以使用 ES6解构语法和内置函数,如Object.assign, Object.values, 并Array#find以声明方式转换数据,如下所示:

const data = [{id:131131,title:"Completed members",memberDetail:{"0":{activeMember:false,memberDetails:{location:"Mumbai",Country:"India"}},"1":{activeMember:true,memberDetails:{location:"London",Country:"England"}},"2":{activeMember:false,memberDetails:{location:"Colombo",Country:"Sri Lanka"}}}},{id:130500,title:"Acting members",memberDetail:{"0":{activeMember:false,memberDetails:{location:"Paris",Country:"France"}},"1":{activeMember:false,memberDetails:{location:"Katmandu",Country:"Nepal"}},"2":{activeMember:true,memberDetails:{location:"Dublin",Country:"Ireland"}}}},{id:131096,title:"Pending members",memberDetail:{"0":{activeMember:true,memberDetails:{location:"Perth",Country:"Australia"}},"1":{activeMember:false,memberDetails:{location:"Berlin",Country:"Germany"}}}}]


const result = data.map(
  ({ id, title, memberDetail }) => Object.assign({ id, title }, Object(
    Object.values(memberDetail).find(m => m.activeMember)
  ).memberDetails)
)

console.log(result)
.as-console-wrapper { min-height: 100% }

于 2017-07-27T02:01:09.247 回答