在Typescript中拆分数组字符串

时间:2018-12-06 16:28:22

标签: javascript

我目前正在得到一串包含值的数组。

目前,所有值都包装在一起,但是我希望每个值都在自己的行上。目前,我只是通过映射(如

)返回字符串数组的值
return supplierRepository.allSuppliers.map((suppliers: ISupplier) => {
    suppliers.name.replace("","<br />")

    console.log(suppliers.name)

    return suppliers.name
  })

如何拆分? In this image, 'thing' should be on a new line

然后我希望每个对象都成为列表中可点击的对象

1 个答案:

答案 0 :(得分:0)

您无法以所需的方式从字符串中分离出“东西”。

JavaScript不够聪明,无法直觉将字符串分割的位置;您需要告诉它一些规则或其他规则来指示它在哪里拆分。

如果始终是"thing"后面附加的字符串supplier.name,则可以使用一段如下的代码使用"thing"的长度在该点进行拆分:< / p>

let supplierName = "ZZ Trinity Merchantsthing"; // This is your string to have <br> inserted
let appendedString = "thing"; // This is the string you want to split into a new line
let brInsertPos = appendedString.length - supplierName.length; // This calculates where to insert the <br> tag

// These lines insert the tag where we worked out it should go
let resultString = supplierName.substr(0, brInsertPos);
resultString += "<br>";
resultString += supplierName.substr(brInsertPos);