javascript - 如何最好地解析成分列表中的每一项,并根据每个解析结果创建一个新对象?
问题描述
我有这个成分列表,我正在尝试制作一个正则表达式来寻找1 cup
,或者 1tsp
或 1 汤匙等等......
我已经制作了这个正则表达式,但它也不起作用。我正在尝试将成分与测量结果分开。
因此,使用此字符串1 Chopped Tomato
,它应该取出1
as 金额并输出:
const output = [
{
val: "Chopped Tomato",
amount: "1",
},
有了下面的这个字符串,它应该能够从中取出½ tsp
并½ tsp fine salt
输出:
const output = [
{
val: "fine sea salt",
amount: "½ tsp",
},
这些是我用于测量的值:
const measures = [
"tbsp","tablespoon","tsp","teaspoon","oz","ounce","fl. oz","fluid ounce","cup","qt",
"quart","pt","pint","gal","gallon","mL","ml","milliliter","g","grams","kg","kilogram","l","liter",
];
这是我构建的输入和正则表达式
const Ingris = [
"1 teaspoon heavy cream",
"1 Chopped Tomato",
"1/2 Cup yogurt",
"1 packet pasta ",
"2 ounces paprika",
]
const FilterFunction = (term) => {
let data = []
if (term) {
const newData = Ingris.filter(({
ingridients
}) => {
if (RegExp(term, "gim").exec(ingridients))
return ingridients.filter(({
val
}) =>
RegExp(term, "gim").exec(val)
).length;
})
data.push(newData)
} else {
data = []
}
};
console.log(FilterFunction("cup"))
期望的输出:
const output = [
{
val: "Tomato",
amount: "1 Chopped ",
},
{
val: "yogurt",
amount: "1/2 Cup",
},
{
val: "1",
amount: "packet pasta ",
},
{
val: "fine sea salt",
amount: "½ tsp",
},
{
val: "heavy cream",
amount: "1/2 teaspoon",
},
{
val: "paprika",
amount: "2 ounces",
},
];
解决方案
这是当我添加数据包和盎司(复数)时起作用的东西
它处理
- 只是数量如 1、2、¼、½、¾ 和 1/2
- 只是没有数量的词,例如“碎肉”
- 复合度量,例如单复数的“液量盎司”
- 切碎或磨碎等动作词
全部由一个半正则表达式和一个解构赋值处理
const measures = [
"tbsp", "tablespoon", "tsp", "teaspoon", "oz", "ounce", "ounces", "cup", "qt", "packet", "quart", "pt", "pint", "gal", "gallon", "mL", "ml", "milliliter", "g", "grams", "kg", "kilogram", "l", "liter",
"fl. oz", "fluid ounce", "fluid ounces" ]; // plural after singular!
const action = ["chopped","ground"]
const compound = measures.filter(measure => measure.split(" ").length > 1); // extract compound words
const amountRe = /^(\d+\/\d+|¼|½|¾|\d|\d+)/; // amounts like 1, 1/2 etc
const amountValueRe = /(\d+\/\d+|¼|½|¾|\d|\d+) ([\w.]+) (.*)/; // first part must be the same as amountRe
const makeList = list => list.map(line => {
if (!amountRe.test(line)) return { value: line }; // no amounts found
// test for compound measures
compound.forEach(cmp => line = line.replace(cmp, cmp.split(" ").join("_"))); // add underscores if found
// destruct the match on amount plus value or amount of amount plus value
let [, num, measure, what] = line.match(amountValueRe);
if (action.includes(measure.toLowerCase())) { // test for chopped
what = `${measure} ${what}`; // or add an action item to the object
measure = "";
}
const obj = {}
if (num) obj.amount = num;
if (measure) obj.measure = measure.split("_").join(" ").trim(); // remove added underscores
if (what) obj.value = what;
return obj;
});
const Ingris = [
"Chicken breast",
"Ground ginger",
"1 teaspoon heavy cream",
"2 fluid ounces lemon juice",
"1 Chopped Tomato",
"1/2 Cup yogurt",
"2 fl. oz paprika",
"1 fluid ounce water",
"½ packet pasta ",
"2 ounces paprika"
];
console.log(makeList(Ingris))
推荐阅读
- android - React Native - 从 android 内容 uri 获取文件名
- go - 从 ffmpeg image2pipe 保存连续的图像流
- react-native - 如何在 react-native 中编码字符串?
- python - 将列表项与对列表进行比较,并输出匹配对
- angular - NGRX 存储 - 服务器上的数据更改未应用于状态
- c# - 验证我收到的是否是整数
- reactjs - 赛普拉斯测试 - redux 商店是空的
- laravel - 从 Outlook 帐户获取电子邮件时出现“无消息”Imap Laravel 错误?
- c# - 在 Asp Net MVC 中排除来自 IdentityUser 的电子邮件
- javascript - 我对 react-modal 有一个奇怪的问题