'how would I create a program that would sort text files like this in js [closed]
Lets say I have a text file structured like this:
Newborn adj. Recently born.
Newly adv. 1 recently. 2 afresh, anew.
New wave n. A style of rock music.
how would I separate the word and the definition in two different text files for the whole array so that I would have a words.txt file in javascript
Solution 1:[1]
You need to separate each line of the text with the separators ["n.","adv.","adj." ...etc.] Look at your file to identify all of them, read the file line by line and use split() by one of these separators.
Demo:
const myText = `Newborn adj. Recently born.
Newly adv. 1 recently. 2 afresh, anew.
New wave n. A style of rock music.
`;
const getSeparator = (separators, target) => {
for (const separator of separators) {
if (target.includes(separator)) return separator;
}
};
const separators = [`adj.`, `adv.`, `n.`];
for (const line of myText.split(`\n`)) {
console.log(line);
const separator = getSeparator(separators, line);
const splitLines = line.split(separator).map((s) => s.trim());
console.log(splitLines);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
