输出是字符串,我想从数组中获取数据

时间:2019-09-01 13:06:58

标签: javascript

所以我从一个数组中获取了索引,并且它返回了多个索引。我希望这些索引从另一个数组中获取数据。

  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ě”,…]

不带重音:

  

[“ ahoj”,“ test1”,“ test2”,“ test3”,“ test4”,“ test5”,“ muzete”,   “ nebo”,“ pak”,“ postupne”,…]   输入是用户输入的类型

现在我从不带Accent的索引中获取索引,我想获取数据取决于来自带有Accent的索引

因此,第一个日志返回了withAccent []位置的字符串,第二个日志正常运行,但是我希望数据不带AccentAccentPosition。

1 个答案:

答案 0 :(得分: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]);

如果您需要直接值,则直接与Accent [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]);