JavaScript:获取句子中单词及其索引

4
我需要获取一个句子中所有单词及其在句子中的索引位置。同一个单词可能在句子中出现多次。
我曾试图使用过滤方法进行此操作,但是索引指示的是数组中的位置而不是句子中的位置。
var sentence = "This is a short sentence, a demo sentence."

sentence.split(" ").filter((word, index) => {
    
}
3个回答

4
您可以使用.reduce方法:

const sentence = "This is a short sentence, a demo sentence.";
let index = 0;
const nonAlphabeticWithoutSpace = /[^a-zA-Z ]/g;

const res = sentence.split(" ").reduce((acc, item) => {
  // get word without other characters
  const word = item.replace(nonAlphabeticWithoutSpace, "");
  // get prev indices of this word
  const wordIndices = acc[word];
  // create/update the indices list
  acc[word] = wordIndices ? [...wordIndices, index] : [index]; 
  // increment the index
  const nonAlphabetic = item.match(nonAlphabeticWithoutSpace);
  index += nonAlphabetic 
    ? word.length+nonAlphabetic.length+1 
    : word.length+1;
  return acc;
}, {});

console.log(res);

如果您希望忽略大小写,请使用.toLowerCase()

谢谢,这是一个不错的解决方案,但另一个答案更接近我所做的事情。 - doorman

2
使用map而不是filter,循环数组并通过计算字母数来查找位置。

...以及它们在句子中的索引位置。

通过索引,我指的是单词在句子中开始的位置。

var sentence = "This is a short sentence, a demo sentence."
let position = 0;
// the words array
const wordsList = sentence.split(" ");

// get the word and position in sentence
const wordDetails = wordsList.map((word, index) => {
  // +1 to account for space
  position = index && wordsList[index - 1].length + position + 1;
  return {
    word,
    position
  };
});

console.log(wordDetails);

如果你需要不同的输出格式,比如 { <word> : [pos1, pos2] },请参考下面的示例。

var sentence = "This is a short sentence, a demo sentence."
let position = 0;
// the words array
const wordsList = sentence.split(" ");
let wordDetails = {};

// get the word and position in sentence
wordsList.forEach((word, index) => {
  // +1 to account for space
  position = index && wordsList[index - 1].length + position + 1;

  // check if entry exists
  if (!wordDetails[word]) {
    wordDetails[word] = [];
  }

  // insert the position in array
  wordDetails[word].push(position)
});

console.log(wordDetails);


0

  // below code will execute an object with key of
  // the index and value 
  
  const sentence = "This is a short sentence, a demo sentence."
        let result = {}
        sentence.split(" ").forEach((word, index) => {
            result = {
                ...result,
                [index]: word
            }
        })
        console.log(result)
        /*
        the result will be like below:
        {
            0: "This"
            2: "a"
            1: "is"
            4: "sentence,"
            3: "short"
            6: "demo"
            5: "a"
            7: "sentence."
        }
        */


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接