从 Javascript 中的数组中搜索和检索匹配数据
Search and retrieve matching data from an array in Javascript
我有一个包含一些数据的数组,如下所示:
我正在尝试实现这一点,以便根据传递的值显示相应的结果。例如,如果我通过 "test-1",它应该 return test, test * 和 test-1。它不应该 return test-2 或 test 3
条件:
- 如果确切的输入值与数组中的某些内容匹配,则应将其添加到结果中
- 如果输入的值有连字符,那么它应该检索不包含连字符但包含连字符之前的文本的任何其他值。
我考虑过使用正则表达式,但不确定这是否是最佳解决方案。到目前为止,我的逻辑感觉它最终会变得混乱,所以我想知道一种优雅的实现方式。
这就是我的
const arrayList = ["test", "hello", "blessed", "blessed-9","test*", "test-1", "blessed*", "test-2", "hello*", "test-3" ];
let result = [];
function tester(inputVal){
arrayList.filter(arrayVal => {
if(arrayVal === inputVal){
result.push(arrayVal);
} else if(arrayVal.indexof('*') !== -1){
//check if the first part matches
}
})
console.log(result,"====")
}
tester("test");
您可以根据指定的条件对 arrayList
使用单个 .filter()
调用。
测试值是否匹配。
如果不是,请测试连字符之前的字符串值是否与元素的开头匹配,以及该元素中是否没有连字符。
在代码段中,通过调用 string.split('-')
并从返回的数组中获取第一个结果来检索连字符之前的字符串值。
const arrayList = ["test", "hello", "blessed", "blessed-9","test*", "test-1", "blessed*", "test-2", "hello*", "test-3" ];
function filterArray(string) {
let root = string.split('-')[0];
return arrayList.filter(element =>
element === string ||
element.startsWith(root) && !element.includes('-'));
}
const result = filterArray('test-1');
console.log(result);
const arrayList = ["test", "hello", "blessed", "blessed-9","test*", "test-1", "blessed*", "test-2", "hello*", "test-3" ];
let result = [];
function tester(inputVal){
result=[]
arrayList.forEach(arrayVal => {
if(arrayVal.search(inputVal)!=-1){
result.push(arrayVal);
}
})
console.log(result)
}
tester("test");
我有一个包含一些数据的数组,如下所示:
我正在尝试实现这一点,以便根据传递的值显示相应的结果。例如,如果我通过 "test-1",它应该 return test, test * 和 test-1。它不应该 return test-2 或 test 3
条件:
- 如果确切的输入值与数组中的某些内容匹配,则应将其添加到结果中
- 如果输入的值有连字符,那么它应该检索不包含连字符但包含连字符之前的文本的任何其他值。
我考虑过使用正则表达式,但不确定这是否是最佳解决方案。到目前为止,我的逻辑感觉它最终会变得混乱,所以我想知道一种优雅的实现方式。
这就是我的
const arrayList = ["test", "hello", "blessed", "blessed-9","test*", "test-1", "blessed*", "test-2", "hello*", "test-3" ];
let result = [];
function tester(inputVal){
arrayList.filter(arrayVal => {
if(arrayVal === inputVal){
result.push(arrayVal);
} else if(arrayVal.indexof('*') !== -1){
//check if the first part matches
}
})
console.log(result,"====")
}
tester("test");
您可以根据指定的条件对 arrayList
使用单个 .filter()
调用。
测试值是否匹配。
如果不是,请测试连字符之前的字符串值是否与元素的开头匹配,以及该元素中是否没有连字符。
在代码段中,通过调用 string.split('-')
并从返回的数组中获取第一个结果来检索连字符之前的字符串值。
const arrayList = ["test", "hello", "blessed", "blessed-9","test*", "test-1", "blessed*", "test-2", "hello*", "test-3" ];
function filterArray(string) {
let root = string.split('-')[0];
return arrayList.filter(element =>
element === string ||
element.startsWith(root) && !element.includes('-'));
}
const result = filterArray('test-1');
console.log(result);
const arrayList = ["test", "hello", "blessed", "blessed-9","test*", "test-1", "blessed*", "test-2", "hello*", "test-3" ];
let result = [];
function tester(inputVal){
result=[]
arrayList.forEach(arrayVal => {
if(arrayVal.search(inputVal)!=-1){
result.push(arrayVal);
}
})
console.log(result)
}
tester("test");