Javascript 我使用map方法不断得到未定义的错误

Javascript 我使用map方法不断得到未定义的错误,javascript,dictionary,methods,Javascript,Dictionary,Methods,我想使用map方法将ing数组更改为newIng,在这里我更改了单位 const unitsLong = ['tablespoons', 'tablespoon', 'ounces', 'ounce', 'teaspoons', 'teaspoon', 'cups', 'pounds']; const unitsShort = ['tbsp', 'tbsp', 'oz', 'oz', 'tsp', 'tsp', 'cup', 'pound']; let ing = [ "8 ounces cr

我想使用map方法将ing数组更改为newIng,在这里我更改了单位

const unitsLong = ['tablespoons', 'tablespoon', 'ounces', 'ounce', 'teaspoons', 'teaspoon', 'cups', 'pounds'];
const unitsShort = ['tbsp', 'tbsp', 'oz', 'oz', 'tsp', 'tsp', 'cup', 'pound'];

let ing = [
"8 ounces cream cheese, softened",
"1/4 teaspoon garlic powder",
" teaspoon dried oregano",
" teaspoon dried parsley",
" teaspoon dried basil",
"1 cups shredded mozzarella cheese",
"1 cups parmesan cheese",
"1 cups pizza sauce",
"1/4 cups chopped green bell pepper",
"1/4 cups chopped onion",
"2 ounces sliced pepperoni"
];

const newIng = ing.map(el => {
  unitsLong.forEach((unit, i) => {
    el = el.replace(unit, unitsShort[i]);
    });

});

console.log(newIng);
我的预期结果将是相同的阵列,其中单位被缩短。
谢谢

除了缺少的
return
statemet之外,您可以减少数组并将更改后的字符串作为累加器

var unitsLong=[“大汤匙”、“大汤匙”、“盎司”、“盎司”、“茶匙”、“茶匙”、“杯子”、“磅”],
单位短=['tbsp','tbsp','oz','oz','tsp','tsp','cup','pound'],
ing=[“8盎司奶油奶酪,软化”,“1/4茶匙大蒜粉”,“一茶匙牛至干”,“一茶匙欧芹干”,“一茶匙罗勒干”,“一杯莫扎里拉奶酪丝”,“一杯帕尔马干酪”,“一杯比萨酱”,“1/4杯青椒碎”,“1/4杯洋葱碎”,“2盎司意大利香肠片],
newIng=ing.map(el=>unitsLong.reduce((s,unit,i)=>s.replace(unit,unitsShort[i]),el));

console.log(newIng)
你的
.map
回调没有返回任何错误?@CertainPerformance,我尝试返回ing或newIng。但是我仍然没有得到我的输出