根据关键字 AND 值数组解析数组

时间:2021-05-20 14:06:24

标签: javascript arrays parsing filter

假设我有一个数组和 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);

3 个答案:

答案 0 :(得分: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"]];

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)))

答案 1 :(得分: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 :(得分:1)

使用两个变量:您可以使用该函数并针对 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

相关问题