【问题标题】:Parsing an array based on keyword AND array of values基于关键字和值数组解析数组
【发布时间】:2021-05-20 14:06:24
【问题描述】:

假设我有一个数组和 2 个变量;一个是简单的字符串,一个是字符串值数组:

var street = "Fernwood Avenue";
var fips = ["34011", "34007"];

var test_array =[["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"],
["07131150", "0", "0.3", "34013", "Essex", "Local", "Beacon Street"],
["03041026", "0", "0.13", "34005", "Burlington", "Local", "Beacon Street"],
["20121109", "0.18", "0.43", "34039", "Union", "Local", "Jones Lane"],
["15141139", "0", "0.27", "34029", "Ocean", "Local", "Jones Lane"]];

我在这里要做的是遍历我的 test_array 并在 sub_array[6] 中找到与 street 匹配的子数组,并且在 fips 列表中匹配 sub_array[3] (["34011", "34007" ])。有没有办法使用过滤器(或任何其他方法)来实现这一点并返回以下结果?

var new_array = [["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"], 
["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"]];

*奖励:假设有一个函数,其中街道和 fips 作为列表参数进入,其中街道名称将始终存在,但 fips 值可能会或可能不会。所以,

var keyword = ["Fernwood Avenue", "34011", "34007"];

var keyword = ["Fernwood Avenue"];

var keyword = ["Fernwood Avenue", "34011"];

使用类似这样的函数(*这不适用于场景 1)

function matcher(array1, array2) {
    return array1.every(value => array2.includes(value));
}

function array_parser(array, keywords) {
    return array.filter(values => matcher(keywords, values));
}

var new_array = array_parser(test_array, keywords);

【问题讨论】:

    标签: javascript arrays parsing filter


    【解决方案1】:

    您的第一个问题是一个简单的过滤器

    var street = "Fernwood Avenue";
    var fips = ["34011", "34007"];
    
    var test_array =[["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
    ["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
    ["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"],
    ["07131150", "0", "0.3", "34013", "Essex", "Local", "Beacon Street"],
    ["03041026", "0", "0.13", "34005", "Burlington", "Local", "Beacon Street"],
    ["20121109", "0.18", "0.43", "34039", "Union", "Local", "Jones Lane"],
    ["15141139", "0", "0.27", "34029", "Ocean", "Local", "Jones Lane"]];
    
    const result = test_array.filter( values => values[6] == street && fips.includes(values[3]))
    console.log(result);

    关于你的额外问题:

    var test_array =[["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
    ["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
    ["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"],
    ["07131150", "0", "0.3", "34013", "Essex", "Local", "Beacon Street"],
    ["03041026", "0", "0.13", "34005", "Burlington", "Local", "Beacon Street"],
    ["20121109", "0.18", "0.43", "34039", "Union", "Local", "Jones Lane"],
    ["15141139", "0", "0.27", "34029", "Ocean", "Local", "Jones Lane"]];
    
    
    function matcher(array1, array2) {
        const street = array1[0];
        const fips = array1.slice(1);
        return array2[6] == street && (fips.length === 0 || fips.includes(array2[3]));
    }
    
    function array_parser(array, keywords) {
        return array.filter(values => matcher(keywords, values));
    }
    
    console.log(array_parser(test_array, ["Fernwood Avenue", "34011", "34007"]));
    
    console.log(array_parser(test_array, ["Fernwood Avenue", "34011"]));
    
    console.log(array_parser(test_array, ["Fernwood Avenue"]));

    【讨论】:

    • 所有这些答案都很棒。我去了这个,因为在我的大型应用程序的上下文中,匹配器功能发挥了最大的作用。谢谢大家!
    【解决方案2】:

    这回答了问题的第一部分

    var street = "Fernwood Avenue";
    var fips = ["34011", "34007"];
    
    var test_array =[["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
    ["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
    ["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"],
    ["07131150", "0", "0.3", "34013", "Essex", "Local", "Beacon Street"],
    ["03041026", "0", "0.13", "34005", "Burlington", "Local", "Beacon Street"],
    ["20121109", "0.18", "0.43", "34039", "Union", "Local", "Jones Lane"],
    ["15141139", "0", "0.27", "34029", "Ocean", "Local", "Jones Lane"]];
    
    console.log(test_array.filter(e => e[6]===street && (fips.indexOf(e[3]) > -1)))

    这回答了问题的第二部分,但我不确定它的可读性如何,简单地说,Array.indexOf() 的第二个参数指定了在列表中搜索的起始索引。但是如果在keyword 中只指定街道,它总是会返回-1,因为没有第二个元素可以开始搜索。

    添加对keyword.length 的检查通过在长度为 1 时始终返回 true 来解决此问题(又名,忽略 fip 的过滤)

    var keyword = ["Fernwood Avenue","34011"];
    
    var test_array =[["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
    ["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
    ["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"],
    ["07131150", "0", "0.3", "34013", "Essex", "Local", "Beacon Street"],
    ["03041026", "0", "0.13", "34005", "Burlington", "Local", "Beacon Street"],
    ["20121109", "0.18", "0.43", "34039", "Union", "Local", "Jones Lane"],
    ["15141139", "0", "0.27", "34029", "Ocean", "Local", "Jones Lane"]];
    
    console.log(test_array.filter(e => (e[6]===keyword[0]) && (keyword.length > 1 ? keyword.indexOf(e[3],1) > -1 : true)))

    【讨论】:

      【解决方案3】:

      使用两个变量:您可以使用该函数并检查Array#filter() 中的每个变量的已知索引。如果您想在传递一个空列表时匹配所有内容,那么您可以进行一个允许所有内容的模拟查找:

      function findStuff(array, street, fips) {
        const streetIndex = 6;
        const fipsIndex = 3;
        
        let fipsLookup;
        if (fips.length > 0)
          fipsLookup = new Set(fips);
        else
          fipsLookup = { has() { return true; } };
      
        return array.filter(item => 
          item[streetIndex] === street && fipsLookup.has(item[fipsIndex])
        )
      }
      
      var test_array =[
        ["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
        ["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
        ["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"],
        ["07131150", "0", "0.3", "34013", "Essex", "Local", "Beacon Street"],
        ["03041026", "0", "0.13", "34005", "Burlington", "Local", "Beacon Street"],
        ["20121109", "0.18", "0.43", "34039", "Union", "Local", "Jones Lane"],
        ["15141139", "0", "0.27", "34029", "Ocean", "Local", "Jones Lane"]
      ];
      
      var street = "Fernwood Avenue";
      var fips = ["34011", "34007"]
      
      let result = findStuff(test_array, street, fips);
      for (const item of result)
        console.log(JSON.stringify(item)); //more concise display
        
      console.log("-----");
      
      fips = []
      
      result = findStuff(test_array, street, fips);
      for (const item of result)
        console.log(JSON.stringify(item)); //more concise display

      *奖励:假设有一个函数,其中街道和 fips 作为列表参数进入,其中街道名称将始终存在,但 fips 值可能会或可能不会。

      从此更改函数声明:

      function findStuff(array, street, fips)
      

      到这里:

      function findStuff(array, [street, ...fips])
      

      为了利用destructuring 将第一项作为街道,将collect the rest into an array 作为fips。函数的主体保持不变。

      function findStuff(array, [street, ...fips]) {
        const streetIndex = 6;
        const fipsIndex = 3;
        
        let fipsLookup;
        if (fips.length > 0)
          fipsLookup = new Set(fips);
        else
          fipsLookup = { has() { return true; } };
      
        return array.filter(item => 
          item[streetIndex] === street && fipsLookup.has(item[fipsIndex])
        )
      }
      
      var test_array =[
        ["06101123", "0", "0.2", "34011", "Cumberland", "Local", "Fernwood Avenue"],
        ["02271163", "0.1", "0.22", "34003", "Bergen", "Local", "Fernwood Avenue"],
        ["04351186", "0.3", "0.59", "34007", "Camden", "Local", "Fernwood Avenue"],
        ["07131150", "0", "0.3", "34013", "Essex", "Local", "Beacon Street"],
        ["03041026", "0", "0.13", "34005", "Burlington", "Local", "Beacon Street"],
        ["20121109", "0.18", "0.43", "34039", "Union", "Local", "Jones Lane"],
        ["15141139", "0", "0.27", "34029", "Ocean", "Local", "Jones Lane"]
      ];
      
      var keywords = ["Fernwood Avenue", "34011", "34007"];
      
      let result = findStuff(test_array, keywords);
      for (const item of result)
        console.log(JSON.stringify(item)); //more concise display
        
      console.log("-----");
      
      keywords = ["Fernwood Avenue"];
      
      result = findStuff(test_array, keywords);
      for (const item of result)
        console.log(JSON.stringify(item)); //more concise display

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-14
        相关资源
        最近更新 更多