过滤数组键

时间:2018-04-07 09:56:18

标签: javascript arrays object

我有一个对象体,我想从中获取is key的值。 如何在最小计算中获取membershipIdserviceIduserId

我只想要数据只是我不想使用过滤器3次

var body = {
  "custom_fields": [{
    "label": "membership_id",
    "data": "1",
  }, {
    "label": "service_id",
    "data": "1",

  }, {
    "label": "user_id",
    "data": "26134",
  }]
};

var membershipInfo = body.custom_fields.filter(n=>{
  return n.label == "membership_id";
});

var serviceInfo = body.custom_fields.filter(n=>{
  return n.label == "service_id";
})
var userInfo = body.custom_fields.filter(n=>{
  return n.label == "user_id";
})

var membershipId = membershipInfo[0].data;
var serviceId    = serviceInfo[0].data;
var userId       = userInfo[0].data;

console.log(membershipId,serviceId,userId);

5 个答案:

答案 0 :(得分:1)

您可以为数组中的所有对象使用循环:



var body = {
    "custom_fields": [{
        "label": "membership_id",
        "data": "1",
    }, {
        "label": "service_id",
        "data": "1",

    }, {
        "label": "user_id",
        "data": "26134",
    }]
};
var res = [];

body.custom_fields.forEach((item)=> {
 var obj = {};
 obj[item['label']] = item['data'];
 res.push(obj);
});
console.log(res);




答案 1 :(得分:1)

  

...在最小计算中

好吧,反复循环遍历数组并不是最小的。你使用一个循环,可能是一个相当无聊的循环:

var membershipId, serviceId, userId;
var fields = body.custom_fields;
for (var i = 0, len = fields.length; i < len; ++i) {
    var entry = fields[i];
    switch (entry && entry.label) {
        case "membership_id":
            membershipId = entry.data;
            break;
        case "service_id":
            serviceId = entry.data;
            break;
        case "user_id":
            userId = entry.data;
            break;
    }
}

直播示例:

&#13;
&#13;
var body = {
    "custom_fields": [{
        "label": "membership_id",
        "data": "1",
    }, {
        "label": "service_id",
        "data": "1",

    }, {
        "label": "user_id",
        "data": "26134",
    }]
};

var membershipId, serviceId, userId;
var fields = body.custom_fields;
for (var i = 0; i < fields.length; ++i) {
    var entry = fields[i];
    switch (entry && entry.label) {
        case "membership_id":
            membershipId = entry.data;
            break;
        case "service_id":
            serviceId = entry.data;
            break;
        case "user_id":
            userId = entry.data;
            break;
    }
}

console.log(membershipId, serviceId, userId);
&#13;
&#13;
&#13;

没有令人兴奋,但你在最小的计算中说过#34;&#34; : - )

这不是最简单的简洁方式,但它可能是运行效率最高的方式。你可以调整:

  • 如果您知道永远不会有nullundefined条目,则可以删除entry &&

    中的switch部分
  • 如果有很多条目无法与任何案例相匹配,如果您已经得到所有需要,可能会提早停止

答案 2 :(得分:1)

您可以使用一个对象并收集所有数据,以便以后与标签一起使用。

var body = { custom_fields: [{ label: "membership_id", data: "1" }, { label: "service_id", data: "1" }, { label: "user_id", data: "26134" }] },
    allData = Object.create(null);

body.custom_fields.forEach(({ label, data }) => allData[label] = data);

console.log(allData);

答案 3 :(得分:1)

您可以创建一个Map对象并从中获取值:

let body = {"custom_fields": [{"label": "membership_id","data": "1",}, {"label": "service_id", "data": "1"}, {"label": "user_id","data": "26134"}]},
    map = new Map(body["custom_fields"].map(o => [o["label"], o["data"]]));

let membershipId = map.get("membership_id");
let serviceId    = map.get("service_id");
let userId       = map.get("user_id");

console.log(membershipId, serviceId, userId);

答案 4 :(得分:1)

Reduce很方便:

const result = body.custom_fields
.reduce((acc, entry) =>
  (acc[entry.label] = acc[entry.value], acc)
, {})