You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

获取匹配指定字符串的数组/映射条目及关联数组过滤咨询

Hey there! Let's tackle your two questions and fix that filtering issue you ran into with .find()—I’ve been there before, so I know how frustrating it can be when things don’t work as expected.

1. 获取所有匹配指定字符串的数组/映射条目

针对普通数组(Array)

If you're working with a simple array of strings (or even objects), the .filter() method is your go-to—it returns all elements that match your condition, unlike .find() which only gives the first match.

示例:字符串数组匹配包含指定内容的元素

const fruits = ["apple", "banana", "cherry", "apple pie"];
const searchTerm = "apple";
const matches = fruits.filter(fruit => fruit.includes(searchTerm));

console.log(matches); // 输出: ["apple", "apple pie"]

示例:对象数组匹配属性值

If your array has objects, just target the specific property you want to check:

const users = [
  { id: 1, username: "alice123" },
  { id: 2, username: "bobsmith" },
  { id: 3, username: "alice_martin" }
];
const searchName = "alice";
const matchingUsers = users.filter(user => user.username.includes(searchName));

console.log(matchingUsers); // 输出: [{ id: 1, username: "alice123" }, { id: 3, username: "alice_martin" }]

针对映射(Map)

Maps don't have a built-in filter method, but you can convert them to an array of entries first, filter, then convert back if needed:

const productMap = new Map([
  [101, "Wireless Headphones"],
  [102, "Wired Earbuds"],
  [103, "Wireless Charger"]
]);
const searchTerm = "Wireless";

// 转换为条目数组并过滤
const filteredEntries = Array.from(productMap.entries()).filter(([id, name]) => name.includes(searchTerm));
// 转回Map(如果需要)
const filteredMap = new Map(filteredEntries);

console.log(filteredMap); // 输出: Map(2) { 101 => "Wireless Headphones", 103 => "Wireless Charger" }

2. 处理一一对应的arrayX和arrayY

You mentioned creating a key-value array/map and using .find() but not getting the expected result—this is almost certainly because .find() only returns the first matching element, not all of them. Let's fix that with .filter() instead.

方法一:创建键值对数组后过滤

First, pair up your arrays into an array of objects, then filter and extract the corresponding arrayY values:

const arrayX = ["apple", "banana", "cherry", "apple"];
const arrayY = [10, 20, 30, 40];
const searchTerm = "apple";

// 把两个数组合并成键值对数组
const pairedItems = arrayX.map((xVal, index) => ({ x: xVal, y: arrayY[index] }));

// 过滤出x匹配的项,再提取对应的y值
const matchingYValues = pairedItems.filter(item => item.x.includes(searchTerm)).map(item => item.y);

console.log(matchingYValues); // 输出: [10, 40]

方法二:直接遍历(更高效)

If you don't want to create an extra array, you can loop through directly and collect matching arrayY values:

const arrayX = ["apple", "banana", "cherry", "apple"];
const arrayY = [10, 20, 30, 40];
const searchTerm = "apple";

const matchingYValues = [];
for (let i = 0; i < arrayX.length; i++) {
  if (arrayX[i].includes(searchTerm)) {
    matchingYValues.push(arrayY[i]);
  }
}

console.log(matchingYValues); // 输出: [10, 40]

Why .find() didn't work?

Just to clarify, here's what happens if you use .find() instead—it only gives you the first match, not all:

const firstMatch = pairedItems.find(item => item.x.includes(searchTerm));
console.log(firstMatch.y); // 输出: 10 (only the first matching Y value)

So if you need all corresponding arrayY elements, .filter() is the right tool for the job.


内容的提问来源于stack exchange,提问作者Bruno

火山引擎 最新活动