如何将字符串转换为采用元素并在每个元素之后组合它们的数组?
How to convert a string to an array that takes the elements and combines them after each element?
所以如果我有一个看起来像这样的字符串:
const name = "Matt"
并且想要创建一个如下所示的数组:
nameSearchArr = [
0: "M",
1: 'Ma',
2: 'Mat',
3: 'Matt
]
我试图通过创建一个数组并使用 'array-contains' 来解决 Firestores no 'full text search' 问题,这样我就可以搜索名称,并且在输入名称时将匹配 nameSearchArr。有人知道最好的方法吗?提前致谢!
虽然我怀疑这是否是 'full text search' 问题的总体解决方案,但以下代码可以解决问题:
const name = "Matt"
const result = name.split("").map((e, i) => name.slice(0,i+1))
console.log(result)
使用 slice 是一种优雅的方法。
const getStringParts = (word) => {
const result = [];
for (let i = 1; i <= word.length; i++) {
result.push(word.slice(0, i));
}
return result;
}
const name = "Matt";
console.log(getStringParts(name));
所以如果我有一个看起来像这样的字符串:
const name = "Matt"
并且想要创建一个如下所示的数组:
nameSearchArr = [
0: "M",
1: 'Ma',
2: 'Mat',
3: 'Matt
]
我试图通过创建一个数组并使用 'array-contains' 来解决 Firestores no 'full text search' 问题,这样我就可以搜索名称,并且在输入名称时将匹配 nameSearchArr。有人知道最好的方法吗?提前致谢!
虽然我怀疑这是否是 'full text search' 问题的总体解决方案,但以下代码可以解决问题:
const name = "Matt"
const result = name.split("").map((e, i) => name.slice(0,i+1))
console.log(result)
使用 slice 是一种优雅的方法。
const getStringParts = (word) => {
const result = [];
for (let i = 1; i <= word.length; i++) {
result.push(word.slice(0, i));
}
return result;
}
const name = "Matt";
console.log(getStringParts(name));