我正在使用ionic2,typescript / angular2中的对象进行标记。这里是代码。
matchText(array) {
for (var i = 0; i < 1; i++) {
var label = this.labels[i];
var ingredients = label.description.toString().split([',', '(', ')', ' ']);
let ingredientList: string[] = ingredients;
console.log('list', ingredientList);
for (var j = 0; j < ingredientList.length; j++) {
console.log(ingredientList[j]);
if (ingredientList[j] == 'SUGAR') {
this.counter++;
console.log('Match');
}
else {
console.log('No Match');
}
}
if (this.counter > 0) {
//this.counter = 0;
this.navCtrl.push(UnSafePage);
}
else {
this.navCtrl.push(SafePage);
}
}
}
上述代码的输出是
INGREDIENTS
WHEAT FLOUR, SUGAR,
CHOCOLATE LIQUOR,
HYDROGENATED VEGETABLE
OIL(SAL FAT, SHEA BUTTER,
RAPESEED OIL, PALM OIL,
SUNFLOWER OIL), WHOLE MILK
POWDER, SHORTENING
(RAPESEED OIL, PALM OIL),
COCOA BUTTER, SALT, BUTTER,
YEAST, EMULSIFIER
(POLYGLYCEROLPOLYRICINOLEATE,
SOY LECITHIN), ARTIFICIAL FLAVOR,
TRISODIUMPHOSPHATE, LEAVENING
(SODIUM BICARBONATE)
CONTAINS: MILK, WHEAT,
SOYBEANS and SHEANUT.
Manufactured in a facility that uses
EGGS and PEANUTS.
我怎样才能得到这个预期的输出?。基本上我想在每个','旁边留一条新线。
INGREDIENTS
WHEAT FLOUR,
SUGAR,
CHOCOLATE LIQUOR,
HYDROGENATED VEGETABLE
OIL(SAL FAT, SHEA BUTTER,
RAPESEED OIL, PALM OIL,
SUNFLOWER OIL),
WHOLE MILK POWDER,
SHORTENING
(RAPESEED OIL, PALM OIL),
COCOA BUTTER,
SALT,
BUTTER,
YEAST,
EMULSIFIER
(POLYGLYCEROLPOLYRICINOLEATE,
SOY LECITHIN),
ARTIFICIAL FLAVOR,
TRISODIUMPHOSPHATE,
LEAVENING
(SODIUM BICARBONATE)
CONTAINS: MILK,
WHEAT,
SOYBEANS and SHEANUT.
Manufactured in a facility that uses
EGGS and PEANUTS.
答案 0 :(得分:1)
查看此JSbin
const mock = 'string , that , you, want, to, split'
const split = mock.split(',');
split.forEach(t => console.log(`${t} \n`))
根据您的需要,您可能希望防止在括号之间使用逗号的换行符。
来自维基百科。
**