根据特定索引中的 2 个或更多值过滤嵌套数组
Filtering a nested array based on 2 or more values in specific indices
我有一个简单的嵌套数组,如下所示:
var test_array = [["0", "0.1", "4.2", "Kramer Street"], ["1", "0.2", "3.5", "Lamar Avenue"], ["3", "4.2", "7.1", "Kramer Street"]];
每个子数组都有 0 - 3 的索引。假设我想要一个函数来获取索引 2 的值,其中索引 1 =“4.2”,索引 3 =“Kramer Street”。我知道我可以使用类似下面的东西来 return 匹配 ["4.2", "Kramer Street"] 的子数组,但在我的问题中,这很棘手,因为从技术上讲,它 returns 2 个子数组我只想要一个基于指定索引的匹配项:
var test_array = [
["0", "0.1", "4.2", "Kramer Street"],
["1", "0.2", "3.5", "Lamar Avenue"],
["3", "4.2", "7.1", "Kramer Street"]
];
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, ["4.2", "Kramer Street"]);
console.log(new_array);
您可以使用Array.find
var test_array = [
["0", "0.1", "4.2", "Kramer Street"],
["1", "0.2", "3.5", "Lamar Avenue"],
["3", "4.2", "7.1", "Kramer Street"]
];
test_array.find(arr => arr[1] === "4.2" && arr[3] === "Kramer Street")[2]
因为你想过滤一个索引和值匹配的数组,传递一个对象作为第二个参数,其中属性是唯一索引,value是要匹配的字符串。
由于数组索引本身是唯一的,因此对象是最合适的,因为它的属性也必须是唯一的。
比使用组合:Array.prototype.filter, Array.prototype.every and finally match by Object.entries 对:
/**
* Filter Array of sub-Arrays where "indexes have value"
* @param {Array} arr Array to filter
* @param {Object} ob Object with index/value pairs
* @return {Array} A filtered array of subarrays.
*/
const filterByIndexValue = (arr, ob) =>
arr.filter(sub => Object.entries(ob).every(([i, v]) => sub[i] === v));
// DEMO TIME:
const test_array = [
["0", "0.1", "4.2", "Kramer Street"],
["3", "4.2", "7.1", "Kramer Street"], // << get this one
["6", "0.2", "3.5", "Lamar Avenue"],
["99", "4.2", "99.1", "Kramer Street"], // << get this one
];
// Filter test_array where sub-arrays
// index 1 has value "4.2" and
// index 3 has value "Kramer Street"
const new_array = filterByIndexValue(test_array, {
1: "4.2",
3: "Kramer Street"
});
console.log(new_array);
我有一个简单的嵌套数组,如下所示:
var test_array = [["0", "0.1", "4.2", "Kramer Street"], ["1", "0.2", "3.5", "Lamar Avenue"], ["3", "4.2", "7.1", "Kramer Street"]];
每个子数组都有 0 - 3 的索引。假设我想要一个函数来获取索引 2 的值,其中索引 1 =“4.2”,索引 3 =“Kramer Street”。我知道我可以使用类似下面的东西来 return 匹配 ["4.2", "Kramer Street"] 的子数组,但在我的问题中,这很棘手,因为从技术上讲,它 returns 2 个子数组我只想要一个基于指定索引的匹配项:
var test_array = [
["0", "0.1", "4.2", "Kramer Street"],
["1", "0.2", "3.5", "Lamar Avenue"],
["3", "4.2", "7.1", "Kramer Street"]
];
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, ["4.2", "Kramer Street"]);
console.log(new_array);
您可以使用Array.find
var test_array = [
["0", "0.1", "4.2", "Kramer Street"],
["1", "0.2", "3.5", "Lamar Avenue"],
["3", "4.2", "7.1", "Kramer Street"]
];
test_array.find(arr => arr[1] === "4.2" && arr[3] === "Kramer Street")[2]
因为你想过滤一个索引和值匹配的数组,传递一个对象作为第二个参数,其中属性是唯一索引,value是要匹配的字符串。 由于数组索引本身是唯一的,因此对象是最合适的,因为它的属性也必须是唯一的。
比使用组合:Array.prototype.filter, Array.prototype.every and finally match by Object.entries 对:
/**
* Filter Array of sub-Arrays where "indexes have value"
* @param {Array} arr Array to filter
* @param {Object} ob Object with index/value pairs
* @return {Array} A filtered array of subarrays.
*/
const filterByIndexValue = (arr, ob) =>
arr.filter(sub => Object.entries(ob).every(([i, v]) => sub[i] === v));
// DEMO TIME:
const test_array = [
["0", "0.1", "4.2", "Kramer Street"],
["3", "4.2", "7.1", "Kramer Street"], // << get this one
["6", "0.2", "3.5", "Lamar Avenue"],
["99", "4.2", "99.1", "Kramer Street"], // << get this one
];
// Filter test_array where sub-arrays
// index 1 has value "4.2" and
// index 3 has value "Kramer Street"
const new_array = filterByIndexValue(test_array, {
1: "4.2",
3: "Kramer Street"
});
console.log(new_array);