输出是字符串,我想从数组中获取数据
2019-09-01
93
因此,我从一个数组中获取索引,它返回多个索引。我希望这些索引能够从另一个数组中获取数据。
let withAccent = array.map(x => x.TERM);
let withoutAccent = terms
.map(x => x.TERM)
.join(",")
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.split(",");
let withoutAccentPosition = withoutAccent
.map((withoutAccent, idx) =>
withoutAccent.includes(input) ? "withAccent[" + idx + "]" : null
)
.filter(e => e !== null)
.join(", ");
console.log(withoutAccentPosition);
console.log(withAccent[0], withAccent[1], withAccent[22]);
withAccent;
[ "ahoj", "test1", "test2", "test3", "test4", "test5", "můžete", "nebo", "pak", "postupně", … ]
withoutAccent:
[ "ahoj", "test1", "test2", "test3", "test4", "test5", "muzete", "nebo", "pak", "postupne", … ] Input is what user types
现在,我从 withoutAccent 中获取索引,并且我希望根据 with Accent 中的索引获取数据
因此,第一个日志返回 withAccent[] 位置的字符串,第二个日志运行正常,但我想要 withoutAccentPosition 中的数据。
1个回答
如果您需要获取数组,则不应使用 join() 。 join() 方法通过连接数组中的所有元素来创建并返回一个新字符串。
以下是解决方案
var input = 'muzete' ;
var withAccent =[ "ahoj", "test1", "test2", "test3", "test4", "test5", "můžete", "nebo", "pak", "postupně"];
var withoutAccent = [ "ahoj", "test1", "test2", "test3", "test4", "test5", "muzete", "nebo", "pak", "postupne" ];
let withoutAccentPosition = withoutAccent
.map((withoutAccent, idx) =>
withoutAccent.includes(input) ? "withAccent[" + idx + "]" : null
).filter(e => e !== null);
console.log(withoutAccentPosition);
console.log(withAccent[0], withAccent[1], withAccent[22]);
如果您需要直接值,则直接使用 withAccent[idx],而不是将其设为字符串类型
var input = 'muzete' ;
var withAccent =[ "ahoj", "test1", "test2", "test3", "test4", "test5", "můžete", "nebo", "pak", "postupně"];
var withoutAccent = [ "ahoj", "test1", "test2", "test3", "test4", "test5", "muzete", "nebo", "pak", "postupne" ];
let withoutAccentPosition = withoutAccent
.map((withoutAccent, idx) =>
withoutAccent.includes(input) ? withAccent[idx] : null
).filter(e => e !== null);
console.log(withoutAccentPosition);
console.log(withAccent[0], withAccent[1], withAccent[22]);
Rishab
2019-09-01