【问题标题】:Filter an Array that has another array inside it过滤其中包含另一个数组的数组
【发布时间】:2019-12-23 16:27:18
【问题描述】:

我正在尝试过滤数组

const array = [{
    city: "Fullerton",
    routes: ["Route 1", "Route 2"],
    state: "CA"
}, {
    city: "Long Beach",
    routes: ["Route 3", "Route 4"],
    state: "CA"
}, {
    city: "Huntington Beach",
    routes: "Route 1",
    state: "CA"
}];

通过另一个数组:

const routes = ["Route 1", "Route 3"];

但是,我无法过滤原始数组的路由项,因为它有数组和字符串作为其变量。有没有办法使用 routes 数组并过滤原始数组而不考虑变量类型?

另外,我希望这样,如果您选择其中一个路由,即使数组元素包含更多路由,它也会过滤数组。

【问题讨论】:

    标签: javascript arrays filter


    【解决方案1】:

    如果你想要至少一个路由匹配,你可以结合someincludes

    const routes = ["Route 1", "Route 3"];
    const array = [{city: "Fullerton", routes: ["Route 1", "Route 2"], state: "CA"}, {city: "Long Beach", routes: ["Route 3", "Route 4"], state: "CA"}, {city: "Huntington Beach", routes: "Route 1", state: "CA"}];
    
    const filteredArray = array.filter(a =>
      [].concat(a.routes).some(r => routes.includes(r))
    )
    
    console.log(filteredArray)

    如果您需要完全匹配所有路由,includes 就足够了:

    const routes = ["Route 1", "Route 3"];
    const array = [{city: "Fullerton", routes: ["Route 1", "Route 2"], state: "CA"}, {city: "Long Beach", routes: ["Route 3", "Route 4"], state: "CA"}, {city: "Huntington Beach", routes: "Route 1", state: "CA"}];
    
    const filteredArray = array.filter(a => routes.includes(a.routes))
    
    console.log(filteredArray)

    【讨论】:

    • 数组中的第三个元素包含routes属性,它不是数组
    【解决方案2】:

    您可以将字符串转换为数组并检查某些项目是否在routes 中。

    var array = [{ city: "Fullerton", routes: ["Route 1", "Route 2"], state: "CA" }, { city: "Long Beach", routes: ["Route 3", "Route 4"], state: "CA" }, { city: "Huntington Beach", routes: "Route 1", state: "CA" }],
        routes = ["Route 1", "Route 3"],
        result = array.filter(o => [].concat(o.routes).some(s => routes.includes(s)));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 确实,数组中没有元素包含routes属性值["Route 1", "Route 3"]
    【解决方案3】:

    如果routes 中的every 元素对应于routes 的某个元素,您可以filter 您的数组:

    const result = array.filter(f=> 
        Array.isArray(f.routes) ? f.routes.every(r => routes.includes(r)) : false);
    

    一个例子:

    const array = [{
        city: "Fullerton",
        routes: ["Route 1", "Route 2"],
        state: "CA"
    }, {
        city: "Long Beach",
        routes: ["Route 3", "Route 4"],
        state: "CA"
    }, {
        city: "Huntington Beach",
        routes: "Route 1",
        state: "CA"
    }, {
        city: "Huntington Beach 1",
        routes: ["Route 1", "Route 3"],
        state: "CA"
    }
    ];
    
    const routes = ["Route 1", "Route 3"];
    
    const result = array.filter(f=> 
        Array.isArray(f.routes) ? f.routes.every(r => routes.includes(r)) : false);
    
    console.log(result);

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 2019-03-01
      • 1970-01-01
      相关资源
      最近更新 更多