根据提供的值过滤嵌套键值对

时间:2018-03-16 12:50:59

标签: javascript angular dictionary key-value

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

trips = {
"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"
        }]
}]};

我试图过滤掉所有的旅行键值对,它们的位置是[值] =维也纳。

我的预期输出应该是:

trips = {
    "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"}]

        }]

};

请帮我找到正确的方法。我正在尝试以下功能,但卡在中间:

for (var date in trips) {
        var res={}
        for (var index = 0; index < trips[date].length; index++) {
            var data = trips[date][index];

             //rest of the logic here

          }

      }

3 个答案:

答案 0 :(得分:2)

您可以使用Array.reduceArray.filterArray.someObject.keys

const filteredTrips = Object.keys(trips).reduce((tmp, x) => {
  const filtered = trips[x].filter(y => y.place.some(z => z.value === 'Vienna'));

  if (filtered.length) {
    tmp[x] = filtered;
  }

  return tmp;
}, {});

&#13;
&#13;
const trips = {
  "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": 2,
          "value": "Canberra"
        },
        {
          "id": 4,
          "value": "Vienna"
        }
      ],
    },
    {
      "journeyId": 1003,
      "Number": "004",
      "DriverName": "Jack",
      "Transporter": {
        "id": "T3",
        "number": "AN01003",
        "Company": "Trax"
      },
      "place": [{
        "id": 1,
        "value": "Washington DC",
      }, {
        "id": 4,
        "value": "Vienna",
      }],
    }
  ],
  "20180211": [{
      "journeyId": 1004,
      "Number": "005",
      "DriverName": "Jack",
      "Transporter": {
        "id": "T3",
        "number": "AN01013",
        "Company": "Trax"
      },
      "place": [{
          "id": 5,
          "value": "Bridgetown"
        },
        {
          "id": 6,
          "value": "Ottawa"
        },
        {
          "id": 4,
          "value": "Vienna"
        }
      ],

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

const filteredTrips = Object.keys(trips).reduce((tmp, x) => {
  const filtered = trips[x].filter(y => y.place.some(z => z.value === 'Vienna'));

  if (filtered.length) {
    tmp[x] = filtered;
  }

  return tmp;
}, {});

console.log(filteredTrips);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以试试这个

const expected = {};
for (let date in trips) {
  for (let trip of trips[date]) {
    if (trip.place.map(place => place.value).includes('Vienna')) {
      expected[date] = trips[date];
    }
  }
}

一旦你的对象有效,这应该有效。

答案 2 :(得分:0)

让我们假设作者错误输入了输入数据。 这是代码:

function getTripsWithPlaceVanillaJS(trips, place) {
    var result = {};

    for (var key in trips) {
        if (trips.hasOwnProperty(key)) {
            var journeys = trips[key];
            var filteredJourneys = [];

            for (var i = 0; i < journeys.length; i++) {
                var journey = journeys[i];

                for (var j = 0; j < journey.place.length; j++) {
                    if (journey.place[i].value === place) {
                        filteredJourneys.push(journey);
                        break;
                    }
                }
            }
            if (filteredJourneys.length) {
                result[key] = filteredJourneys;
            }
        }
    }

    return result;
}

function getTripsWithPlaceSugaredES6(trips, place) {
    return Object.keys(trips).reduce((result, key) => {
        const filteredJourneys = trips[key].filter(journey => journey.place.some(item => item.value === place));

        if (filteredJourneys.length) {
            result[key] = filteredJourneys;
        }
        return result;
    }, {});
}