javaScript ES6 map()仅返回某个对象

时间:2017-07-26 22:35:42

标签: javascript json loops ecmascript-6 es6-map

javaScript ES6 .map()返回期待包括位置& 城市基于activeMember: true

中的memberDetail



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




**期待结果** enter image description here

2 个答案:

答案 0 :(得分: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
  };
})

应该这样做。

如果没有有效成员locationCountry返回undefined。如果您希望它们在这种情况下返回空字符串,只需写入

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

如果有多个活跃成员,则会选择ID最低的成员。

答案 1 :(得分:0)

您可以使用ES6 destructuring syntax和内置插件,例如Object.assignObject.valuesArray#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% }