从字符串数组中删除第一个单词并存储在一个新变量中
Cut the first word out of an array of strings and store in a new variable
我目前正在取回这样的数组:
['Alica Brereton,Marijuana,9.18,50',
'William Kotai,ecstasy,19.12,20',
'Joel Forro,heroin,91.16,5',
'David Ernest,Methamphetamine,108.78,5',
'David Ernest,cocaine,80,2',
'Joel Forro,ecstasy,19.12,10',
'Gabriella Hyde,Marijuana,9.18,10',
'Gabriella Hyde,Methamphetamine,108.78,8',
'Marijuana,9.18,10'
]
我想在每个字符串的开头剪切名称并存储在一个新变量中,所以我会有这样的东西:
['Alica Brereton',
'William Kotai',
'Joel Forro',
'David Ernest',
'Joel Forro',
'Gabriella Hyde'
]
我正在处理一个文本文件,这里是我的当前文件
code:
const fs = require("fs");
const data = fs.readFileSync("data.txt").toString();
const lines = data.split("\n");
let customerKeys = lines[0].replace("customer", "Total Price");
lines.shift();
console.log(customerKeys);
console.log(lines);
let array = lines.filter((str) => {
return str !== "";
});
let arrNoSpaces = array;
console.log(arrNoSpaces);
arrNoSpaces[6].replace("Gabriella Hyde,", "");
var GabriellaExtra = arrNoSpaces[6].replace("Gabriella Hyde,", "");
console.log(GabriellaExtra);
arrNoSpaces.push(GabriellaExtra);
console.log(arrNoSpaces);
arrNoSpaces.splice(-3, 1);
console.log(arrNoSpaces);
正则表达式可以匹配每个字符串中的字母和空格(不是逗号),映射到一个新数组。
const arr = ['Alica Brereton,Marijuana,9.18,50',
'William Kotai,ecstasy,19.12,20',
'Joel Forro,heroin,91.16,5',
'David Ernest,Methamphetamine,108.78,5',
'David Ernest,cocaine,80,2',
'Joel Forro,ecstasy,19.12,10',
'Gabriella Hyde,Marijuana,9.18,10',
'Gabriella Hyde,Methamphetamine,108.78,8',
'Marijuana,9.18,10'
];
const result = arr
.map(line => line.match(/\w+ \w+/)?.[0]) // extract matches
.filter((item, i, arr) => item && arr[i - 1] !== item) // filter out failed matches and dupes
console.log(result);
我目前正在取回这样的数组:
['Alica Brereton,Marijuana,9.18,50',
'William Kotai,ecstasy,19.12,20',
'Joel Forro,heroin,91.16,5',
'David Ernest,Methamphetamine,108.78,5',
'David Ernest,cocaine,80,2',
'Joel Forro,ecstasy,19.12,10',
'Gabriella Hyde,Marijuana,9.18,10',
'Gabriella Hyde,Methamphetamine,108.78,8',
'Marijuana,9.18,10'
]
我想在每个字符串的开头剪切名称并存储在一个新变量中,所以我会有这样的东西:
['Alica Brereton',
'William Kotai',
'Joel Forro',
'David Ernest',
'Joel Forro',
'Gabriella Hyde'
]
我正在处理一个文本文件,这里是我的当前文件
code:
const fs = require("fs");
const data = fs.readFileSync("data.txt").toString();
const lines = data.split("\n");
let customerKeys = lines[0].replace("customer", "Total Price");
lines.shift();
console.log(customerKeys);
console.log(lines);
let array = lines.filter((str) => {
return str !== "";
});
let arrNoSpaces = array;
console.log(arrNoSpaces);
arrNoSpaces[6].replace("Gabriella Hyde,", "");
var GabriellaExtra = arrNoSpaces[6].replace("Gabriella Hyde,", "");
console.log(GabriellaExtra);
arrNoSpaces.push(GabriellaExtra);
console.log(arrNoSpaces);
arrNoSpaces.splice(-3, 1);
console.log(arrNoSpaces);
正则表达式可以匹配每个字符串中的字母和空格(不是逗号),映射到一个新数组。
const arr = ['Alica Brereton,Marijuana,9.18,50',
'William Kotai,ecstasy,19.12,20',
'Joel Forro,heroin,91.16,5',
'David Ernest,Methamphetamine,108.78,5',
'David Ernest,cocaine,80,2',
'Joel Forro,ecstasy,19.12,10',
'Gabriella Hyde,Marijuana,9.18,10',
'Gabriella Hyde,Methamphetamine,108.78,8',
'Marijuana,9.18,10'
];
const result = arr
.map(line => line.match(/\w+ \w+/)?.[0]) // extract matches
.filter((item, i, arr) => item && arr[i - 1] !== item) // filter out failed matches and dupes
console.log(result);