如何循环嵌套键值对并根据数组值

时间:2018-03-17 12:10:53

标签: javascript dictionary key-value

我有一个根据用户输入构建的数组。我试图循环嵌套的键值对,并检查它中的值是否匹配给定数组的任何值。目的是建立一个搜索工具。

我的阵列:

FilteredSelectedOptions=["20180211","Trax","Vienna","AN01020"]

我的键值对是:

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"
      }
    ],
  }],
};

我期待这样的输出:

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"}]

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

};

我正在尝试使用for循环来检查每个数组元素与键值对。我想要做的基本上是

for (option in FilteredselectedOptions) 
{
     //logic
}

我能够为一个特定的数组值做到这一点:

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

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

  return tmp;
}, {});

但是我如何对所有数组元素执行此操作,而不管数组中有多少元素。请在循环中提供帮助。

3 个答案:

答案 0 :(得分:1)

可以使用递归来搜索对象中的值:

 function hasValue(obj, values){
  for(const value of Object.values(obj)){
    if(typeof value === "object"){
       if(hasValue(value, values))
          return true;
    } else {
       if(values.includes(value))
          return true;
    }
  }
  return false;
 }

现在我们可以通过比较键并使用上面的函数来过滤您的行程:

  const filteredTrips = Object.keys(trips).filter(key => {
    if(FilteredSelectedOptions.includes(key))
       return true;
    if(hasValue(trips[key], FilteredSelectedOptions))
       return true;
    return false;
 });

答案 1 :(得分:1)

尝试通过在数据中查找单个项目进行过滤来实现一种方法。

function filter(object, options) {
    const check = v => options.includes(v) || v && typeof v === 'object' && Object.keys(v).some(l => check(v[l]));
    var result = {};
    Object.keys(object).forEach(function (k) {
        var temp = options.includes(k)
            ? object[k]
            : object[k].filter(check);

        if (temp.length) {
            result[k] = temp;
        }
    });
    return result;
}

var 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" }] }] },
    options = ["20180211", /* "Trax", */ "Vienna", "AN01020"];

console.log(filter(trips, options));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

如果您需要完整的代码,请告诉我。 让我们开始javascript中的对象没有内置过滤器或reduce方法,只有数组才有。我会做什么,我将遍历对象属性,这是数组,并检查它们是否具有FilteredSelectedOptions中找到的属性之一。如果我找到一个,我会把它推到一个包含结果的新数组。