在 TypeScript 上,我试图将两个属性连接到一列中,这可能吗?
例如,我有“Dog”和“Cat”属性,我想将它们显示在 matTable 中名为“Animals”的单列中。
因此,在“动物”列的单个单元格中,我将看到“狗,猫”。
在这里我制作了两个单独的列,但我不确定如何在同一单元格中查看多个属性:
configureColumns() {
this.columns = [
{
key: "Dog",
display: "Dog",
styles: { flex: "0 0 10%" }
},
{
key: "Cat",
display: "Cat",
styles: { flex: "0 0 10%" }
},
试试这个:
configureColumns() {
const columns = [
{
key: "Dog",
display: "Dog",
styles: { flex: "0 0 10%" }
},
{
key: "Cat",
display: "Cat",
styles: { flex: "0 0 10%" }
}];
const initialValue = '';
const sumWithInitial = columns.map(elt => elt.key).reduce(
(accumulator, currentValue) => accumulator + currentValue + " ",
initialValue,
);
console.log(sumWithInitial);
}
另一种解决方案:
const result = columns.map(elt => elt.key).join(" ");
console.log(result);