从所有对象中都具有id的数组中获取对象,该对象在每个对象中作为键值对存储在数组中

时间:2018-09-29 08:21:48

标签: angular typescript

我正在使用角度6打字稿,因为我是新手,所以我需要一些帮助,下面是一系列对象:

[{"roomCode":"BR-017","roomDescription":"Raum 017 Brühl","setups":[{"SetupId":"PC-SCH","SetupDescription":"PC Schulung","minQuantity":1,"maxQuantity":20,"uomCode":"PRS"},{"SetupId":"SONDER","SetupDescription":"Sonderbestuhlung","minQuantity":1,"maxQuantity":10,"uomCode":"PRS"},{"SetupId":"STEHT","SetupDescription":"Stehtische","minQuantity":1,"maxQuantity":40,"uomCode":"PRS"},{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":8,"maxQuantity":18,"uomCode":"PRS"}],"features":[{"FeatureId":"M2","FeatrureDescription":"Fläche in m²","minQuantity":100,"maxQuantity":100,"uomCode":"M2"},{"FeatureId":"PC","FeatrureDescription":"PC","minQuantity":1,"maxQuantity":10,"uomCode":"STK"},{"FeatureId":"TEL","FeatrureDescription":"Telefon","minQuantity":1,"maxQuantity":12,"uomCode":"STK"},{"FeatureId":"WLAN","FeatrureDescription":"WLAN","minQuantity":1,"maxQuantity":15,"uomCode":"STK"}]},{"roomCode":"BR-024","roomDescription":"Raum 024 Brühl","setups":[{"SetupId":"BUFFET","SetupDescription":"Buffet","minQuantity":1,"maxQuantity":12,"uomCode":"PRS"},{"SetupId":"KINO","SetupDescription":"Kinobestuhlung","minQuantity":1,"maxQuantity":12,"uomCode":"PRS"},{"SetupId":"KONF","SetupDescription":"Konferenzbestuhlung","minQuantity":1,"maxQuantity":14,"uomCode":"PRS"},{"SetupId":"LEER","SetupDescription":"ohne Bestuhlung","minQuantity":1,"maxQuantity":17,"uomCode":"PRS"},{"SetupId":"PC-SCH","SetupDescription":"PC Schulung","minQuantity":1,"maxQuantity":33,"uomCode":"PRS"},{"SetupId":"S-KONF","SetupDescription":"schmale Konferenzbestuhlung","minQuantity":1,"maxQuantity":33,"uomCode":"PRS"},{"SetupId":"SONDER","SetupDescription":"Sonderbestuhlung","minQuantity":1,"maxQuantity":13,"uomCode":"PRS"},{"SetupId":"STEHT","SetupDescription":"Stehtische","minQuantity":1,"maxQuantity":12,"uomCode":"PRS"},{"SetupId":"STUHLK","SetupDescription":"Stuhlkreis","minQuantity":9,"maxQuantity":20,"uomCode":"PRS"},{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":22,"uomCode":"PRS"},{"SetupId":"TISCHK","SetupDescription":"Tischkreis","minQuantity":1,"maxQuantity":22,"uomCode":"PRS"},{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":9,"maxQuantity":20,"uomCode":"PRS"}],"features":[{"FeatureId":"8SWI","FeatrureDescription":"8er Switch","minQuantity":0,"maxQuantity":111,"uomCode":"STK"},{"FeatureId":"BEAM","FeatrureDescription":"Beamer","minQuantity":0,"maxQuantity":11,"uomCode":"STK"},{"FeatureId":"BILDS","FeatrureDescription":"Bildschirm","minQuantity":0,"maxQuantity":1,"uomCode":"STK"},{"FeatureId":"FLIP","FeatrureDescription":"Flipchart","minQuantity":0,"maxQuantity":11,"uomCode":"STK"},{"FeatureId":"M2","FeatrureDescription":"Fläche in m²","minQuantity":0,"maxQuantity":11,"uomCode":"M2"},{"FeatureId":"MOD","FeatrureDescription":"Moderationskoffer","minQuantity":0,"maxQuantity":11,"uomCode":"STK"},{"FeatureId":"PC","FeatrureDescription":"PC","minQuantity":0,"maxQuantity":11,"uomCode":"STK"},{"FeatureId":"PINN","FeatrureDescription":"Pinnwand","minQuantity":0,"maxQuantity":11,"uomCode":"STK"},{"FeatureId":"ROLLSTUH","FeatrureDescription":"Zugang für Rollstühle","minQuantity":0,"maxQuantity":11,"uomCode":"STK"},{"FeatureId":"TEKO","FeatrureDescription":"Telefonkonferenz-Anlage","minQuantity":0,"maxQuantity":11,"uomCode":"STK"},{"FeatureId":"TEL","FeatrureDescription":"Telefon","minQuantity":0,"maxQuantity":1,"uomCode":"STK"},{"FeatureId":"TONKL","FeatrureDescription":"Tonanlage klein (Handmikrofon)","minQuantity":0,"maxQuantity":1,"uomCode":"STK"},{"FeatureId":"VK","FeatrureDescription":"Videokonferenzanlage","minQuantity":0,"maxQuantity":11,"uomCode":"STK"},{"FeatureId":"WLAN","FeatrureDescription":"WLAN","minQuantity":0,"maxQuantity":11,"uomCode":"STK"}]},{"roomCode":"BR-025","roomDescription":"Raum 025 Brühl","setups":[{"SetupId":"STUHLK","SetupDescription":"Stuhlkreis","minQuantity":9,"maxQuantity":20,"uomCode":"PRS"},{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":9,"maxQuantity":20,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-101","roomDescription":"Raum 101 Brühl","setups":[{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":8,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-102","roomDescription":"Raum 102 Brühl","setups":[{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":10,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-103","roomDescription":"Raum 103 Brühl","setups":[{"SetupId":"STUHLK","SetupDescription":"Stuhlkreis","minQuantity":9,"maxQuantity":20,"uomCode":"PRS"},{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":9,"maxQuantity":20,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-104","roomDescription":"Raum 104 Brühl","setups":[{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":8,"uomCode":"PRS"},{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":1,"maxQuantity":8,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-105","roomDescription":"Raum 105 Brühl","setups":[{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":8,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-106","roomDescription":"Raum 106 Brühl","setups":[{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":8,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-107","roomDescription":"Raum 107 Brühl","setups":[{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":8,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-108","roomDescription":"Raum 108 Brühl","setups":[{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":8,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-109","roomDescription":"Raum 109 Brühl","setups":[{"SetupId":"TISCHB","SetupDescription":"Tischblock/blöcke","minQuantity":1,"maxQuantity":8,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-114","roomDescription":"Raum 114 Brühl","setups":[{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":3,"maxQuantity":18,"uomCode":"PRS"}],"features":[{"FeatureId":"BEAM","FeatrureDescription":"Beamer","minQuantity":0,"maxQuantity":1,"uomCode":"STK"},{"FeatureId":"FLIP","FeatrureDescription":"Flipchart","minQuantity":0,"maxQuantity":2,"uomCode":"STK"},{"FeatureId":"PINN","FeatrureDescription":"Pinnwand","minQuantity":0,"maxQuantity":3,"uomCode":"STK"}]},{"roomCode":"BR-116","roomDescription":"Raum 116 Brühl","setups":[{"SetupId":"PC-SCH","SetupDescription":"PC Schulung","minQuantity":2,"maxQuantity":12,"uomCode":"PRS"}],"features":[{"FeatureId":"PC","FeatrureDescription":"PC","minQuantity":1,"maxQuantity":12,"uomCode":"STK"}]},{"roomCode":"BR-118","roomDescription":"Raum 118 Brühl","setups":[{"SetupId":"PC-SCH","SetupDescription":"PC Schulung","minQuantity":2,"maxQuantity":12,"uomCode":"PRS"}],"features":[{"FeatureId":"PC","FeatrureDescription":"PC","minQuantity":1,"maxQuantity":12,"uomCode":"STK"}]},{"roomCode":"BR-119","roomDescription":"Raum 119 Brühl","setups":[{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":4,"maxQuantity":20,"uomCode":"PRS"}],"features":[{"FeatureId":"BEAM","FeatrureDescription":"Beamer","minQuantity":0,"maxQuantity":1,"uomCode":"STK"},{"FeatureId":"FLIP","FeatrureDescription":"Flipchart","minQuantity":0,"maxQuantity":2,"uomCode":"STK"},{"FeatureId":"PINN","FeatrureDescription":"Pinnwand","minQuantity":0,"maxQuantity":3,"uomCode":"STK"}]},{"roomCode":"BR-120","roomDescription":"Raum 120 Brühl","setups":[{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":4,"maxQuantity":20,"uomCode":"PRS"}],"features":[{"FeatureId":"BEAM","FeatrureDescription":"Beamer","minQuantity":0,"maxQuantity":1,"uomCode":"STK"},{"FeatureId":"FLIP","FeatrureDescription":"Flipchart","minQuantity":0,"maxQuantity":2,"uomCode":"STK"},{"FeatureId":"PINN","FeatrureDescription":"Pinnwand","minQuantity":0,"maxQuantity":3,"uomCode":"STK"}]},{"roomCode":"BR-121","roomDescription":"Raum 121 Brühl","setups":[{"SetupId":"STUHLK","SetupDescription":"Stuhlkreis","minQuantity":9,"maxQuantity":24,"uomCode":"PRS"},{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":9,"maxQuantity":24,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-122","roomDescription":"Raum 122 Brühl","setups":[{"SetupId":"STUHLK","SetupDescription":"Stuhlkreis","minQuantity":9,"maxQuantity":20,"uomCode":"PRS"},{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":9,"maxQuantity":20,"uomCode":"PRS"}],"features":[]},{"roomCode":"BR-K45","roomDescription":"Kombi R024+R025  Brühl","setups":[{"SetupId":"KINO","SetupDescription":"Kinobestuhlung","minQuantity":21,"maxQuantity":42,"uomCode":"PRS"},{"SetupId":"STUHLK","SetupDescription":"Stuhlkreis","minQuantity":21,"maxQuantity":42,"uomCode":"PRS"},{"SetupId":"U-FORM","SetupDescription":"U-Form","minQuantity":21,"maxQuantity":42,"uomCode":"PRS"}],"features":[]}]

我只想取出所有房间对象阵列中常见的features数组对象。例如,这里带有WLAN的功能对于所有房间对象都是通用的,因此我的结果将具有特色:

[0:{featureId: WLAN featureDesc: wifilan}]

,如果有多个公共对象,则对象数组将相应增加;如果没有公共对象,则该数组为空。 提前致谢 :) enter image description here

1 个答案:

答案 0 :(得分:0)

这有助于将问题分解为多个步骤。

在下面的代码中,我分3个步骤完成了此操作:

  1. 将所有功能提取到一个数组中
  2. 删除所有重复功能
  3. 过滤功能,使我们只剩下常见的功能。

在下面的代码中,我仅包括了您数据中的前2个房间。您应该可以点击Run code snippet来查看结果。

let data = [{
    "roomCode": "BR-017",
    "roomDescription": "Raum 017 Brühl",
    "setups": [{
        "SetupId": "PC-SCH",
        "SetupDescription": "PC Schulung",
        "minQuantity": 1,
        "maxQuantity": 20,
        "uomCode": "PRS"
    }, {
        "SetupId": "SONDER",
        "SetupDescription": "Sonderbestuhlung",
        "minQuantity": 1,
        "maxQuantity": 10,
        "uomCode": "PRS"
    }, {
        "SetupId": "STEHT",
        "SetupDescription": "Stehtische",
        "minQuantity": 1,
        "maxQuantity": 40,
        "uomCode": "PRS"
    }, {
        "SetupId": "U-FORM",
        "SetupDescription": "U-Form",
        "minQuantity": 8,
        "maxQuantity": 18,
        "uomCode": "PRS"
    }],
    "features": [{
        "FeatureId": "M2",
        "FeatrureDescription": "Fläche in m²",
        "minQuantity": 100,
        "maxQuantity": 100,
        "uomCode": "M2"
    }, {
        "FeatureId": "PC",
        "FeatrureDescription": "PC",
        "minQuantity": 1,
        "maxQuantity": 10,
        "uomCode": "STK"
    }, {
        "FeatureId": "TEL",
        "FeatrureDescription": "Telefon",
        "minQuantity": 1,
        "maxQuantity": 12,
        "uomCode": "STK"
    }, {
        "FeatureId": "WLAN",
        "FeatrureDescription": "WLAN",
        "minQuantity": 1,
        "maxQuantity": 15,
        "uomCode": "STK"
    }]
}, {
    "roomCode": "BR-024",
    "roomDescription": "Raum 024 Brühl",
    "setups": [{
        "SetupId": "BUFFET",
        "SetupDescription": "Buffet",
        "minQuantity": 1,
        "maxQuantity": 12,
        "uomCode": "PRS"
    }, {
        "SetupId": "KINO",
        "SetupDescription": "Kinobestuhlung",
        "minQuantity": 1,
        "maxQuantity": 12,
        "uomCode": "PRS"
    }, {
        "SetupId": "KONF",
        "SetupDescription": "Konferenzbestuhlung",
        "minQuantity": 1,
        "maxQuantity": 14,
        "uomCode": "PRS"
    }, {
        "SetupId": "LEER",
        "SetupDescription": "ohne Bestuhlung",
        "minQuantity": 1,
        "maxQuantity": 17,
        "uomCode": "PRS"
    }, {
        "SetupId": "PC-SCH",
        "SetupDescription": "PC Schulung",
        "minQuantity": 1,
        "maxQuantity": 33,
        "uomCode": "PRS"
    }, {
        "SetupId": "S-KONF",
        "SetupDescription": "schmale Konferenzbestuhlung",
        "minQuantity": 1,
        "maxQuantity": 33,
        "uomCode": "PRS"
    }, {
        "SetupId": "SONDER",
        "SetupDescription": "Sonderbestuhlung",
        "minQuantity": 1,
        "maxQuantity": 13,
        "uomCode": "PRS"
    }, {
        "SetupId": "STEHT",
        "SetupDescription": "Stehtische",
        "minQuantity": 1,
        "maxQuantity": 12,
        "uomCode": "PRS"
    }, {
        "SetupId": "STUHLK",
        "SetupDescription": "Stuhlkreis",
        "minQuantity": 9,
        "maxQuantity": 20,
        "uomCode": "PRS"
    }, {
        "SetupId": "TISCHB",
        "SetupDescription": "Tischblock/blöcke",
        "minQuantity": 1,
        "maxQuantity": 22,
        "uomCode": "PRS"
    }, {
        "SetupId": "TISCHK",
        "SetupDescription": "Tischkreis",
        "minQuantity": 1,
        "maxQuantity": 22,
        "uomCode": "PRS"
    }, {
        "SetupId": "U-FORM",
        "SetupDescription": "U-Form",
        "minQuantity": 9,
        "maxQuantity": 20,
        "uomCode": "PRS"
    }],
    "features": [{
        "FeatureId": "8SWI",
        "FeatrureDescription": "8er Switch",
        "minQuantity": 0,
        "maxQuantity": 111,
        "uomCode": "STK"
    }, {
        "FeatureId": "BEAM",
        "FeatrureDescription": "Beamer",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }, {
        "FeatureId": "BILDS",
        "FeatrureDescription": "Bildschirm",
        "minQuantity": 0,
        "maxQuantity": 1,
        "uomCode": "STK"
    }, {
        "FeatureId": "FLIP",
        "FeatrureDescription": "Flipchart",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }, {
        "FeatureId": "M2",
        "FeatrureDescription": "Fläche in m²",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "M2"
    }, {
        "FeatureId": "MOD",
        "FeatrureDescription": "Moderationskoffer",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }, {
        "FeatureId": "PC",
        "FeatrureDescription": "PC",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }, {
        "FeatureId": "PINN",
        "FeatrureDescription": "Pinnwand",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }, {
        "FeatureId": "ROLLSTUH",
        "FeatrureDescription": "Zugang für Rollstühle",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }, {
        "FeatureId": "TEKO",
        "FeatrureDescription": "Telefonkonferenz-Anlage",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }, {
        "FeatureId": "TEL",
        "FeatrureDescription": "Telefon",
        "minQuantity": 0,
        "maxQuantity": 1,
        "uomCode": "STK"
    }, {
        "FeatureId": "TONKL",
        "FeatrureDescription": "Tonanlage klein (Handmikrofon)",
        "minQuantity": 0,
        "maxQuantity": 1,
        "uomCode": "STK"
    }, {
        "FeatureId": "VK",
        "FeatrureDescription": "Videokonferenzanlage",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }, {
        "FeatureId": "WLAN",
        "FeatrureDescription": "WLAN",
        "minQuantity": 0,
        "maxQuantity": 11,
        "uomCode": "STK"
    }]
}]


// Create an array containing all the features
let features = data.reduce((features, room) => [...features, ...room.features], []);

// Remove any duplicates
features = features.filter((feat, idx) => features.findIndex(f => f.FeatureId === feat.FeatureId) === idx)

// Remove any that aren't in every room
features = features.filter(feature => data.every(room => room.features.find(f => f.FeatureId === feature.FeatureId)))

// Log the result
console.log(features);