根据特定值搜索并返回键值对

时间:2018-03-16 08:30:01

标签: javascript angular dictionary hashmap key-value

我是Angular的新手并且是第一次做键值对。我试图根据嵌套键值映射中的特定值获取键值对。我有一个嵌套的JSON数据格式:

journey = {
"20180201": [{
        "journeyId": 1001,
        "Number": "001",
        "DriverName":"Alex",
        "Transporter": {
            "id": "T1",
            "number": "AN01001",
            "Company": "Tranzient"
        }
        "place": [{"id":001,"value":"Washington DC" }]
                  [{"id":002,"value":"Canberra"}]
    }]
    [{
       "journeyId": 1002,
        "Number": "001",
        "DriverName":"Tom",
        "Transporter": {
            "id": "T2",
            "number": "AN01002",
            "Company": "Trax"
        }
        "place": [{"id":002,"value":"Canberra" }]
                  [{"id":004,"value":"Vienna"}]
    }]
    [{
         "journeyId": 1003,
        "Number": "004",
        "DriverName":"Jack",
        "Transporter": {
            "id": "T3",
            "number": "AN01003",
            "Company": "Trax"
        }
        "place": [{"id":001,"value":"Washington DC" }]
                  [{"id":004,"value":"Vienna"}]
    }],
"20180211": [{
         "journeyId": 1004,
        "Number": "005",
        "DriverName":"Jack",
        "Transporter": {
            "id": "T3",
            "number": "AN01013",
            "Company": "Trax"
        }
        "place": [{"id":005,"value":"Bridgetown" }]
                  [{"id":006,"value":"Ottawa"}]
                  [{"id":004,"value":"Vienna"}]

    }]
    [{
       "journeyId": 1005,
        "Number": "005",
        "DriverName":"Jerry",
        "Transporter": {
            "id": "T3",
            "number": "AN01020",
            "Company": "Trax"
        }
        "place": [{"id":005,"value":"Bridgetown" }]
                  [{"id":006,"value":"Ottawa"}]
    }],
"20180301": [{
        "journeyId": 1006,
        "Number": "005",
        "DriverName":"demy",
        "Transporter": {
            "id": "T3",
            "number": "AN01003",
            "Company": "Trax"
        }
        "place": [{"id":005,"value":"Bridgetown" }]
                  [{"id":006,"value":"Ottawa"}]
    }]

};

我正在尝试过滤掉所有具有 place [value] = Vienna 的旅程键值对。

我的预期输出应该是:

journey = {
    "20180201": 
        [{
           "journeyId": 1002,
            "Number": "001",
            "DriverName":"Tom",
            "Transporter": {
                "id": "T2",
                "number": "AN01002",
                "Company": "Trax"
            }
            "place": [{"id":002,"value":"Canberra" }]
                      [{"id":004,"value":"Vienna"}]
        }]
        [{
             "journeyId": 1003,
            "Number": "004",
            "DriverName":"Jack",
            "Transporter": {
                "id": "T3",
                "number": "AN01003",
                "Company": "Trax"
            }
            "place": [{"id":001,"value":"Washington DC" }]
                      [{"id":004,"value":"Vienna"}]
        }],
    "20180211": [{
             "journeyId": 1004,
            "Number": "005",
            "DriverName":"Jack",
            "Transporter": {
                "id": "T3",
                "number": "AN01013",
                "Company": "Trax"
            }
            "place": [{"id":005,"value":"Bridgetown" }]
                      [{"id":006,"value":"Ottawa"}]
                      [{"id":004,"value":"Vienna"}]

        }]

};

我不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

您必须使用javascript中的filter()方法过滤数组。 https://www.w3schools.com/Jsref/jsref_filter.asp

示例:

// your array containing objects
var arr = [{
    id: 2,
    text: 'asdasdasd'
  },
  {
    id: 3,
    text: 'mytext'
  },
  {
    id: 1,
    text: 'mytext'
  }];
// filter array by property 'text' = 'mytext'
var filteredArray = arr.filter(function(item) {
  return item.text === 'mytext';
});

结果你现在有filteredArray

[{
  id: 3,
  text: 'mytext'
},
{
  id: 1,
  text: 'mytext'
}]