我有這個陣列:
data: [
‘big text 1 here.’,
‘big text 2 here followed by list:’,
'-this is list item;',
'-this is another list item;',
‘big text 3 here followed by list:’,
'-this is list item;',
'-this is another list item;',
'-this is another list item;',
‘big text 4 here.’
],
我想把它變成這樣的東西:
data: [
{
text: ‘big text 1 here.’
list: []
},
{
text: ‘big text 2 here followed by list:’
list: [
'-this is list item;',
'-this is another list item;'
]
},
{
text: ‘big text 3 here followed by list:’
list: [
'-this is list item;',
'-this is another list item;',
'-this is another list item;',
]
},
{
text: ‘big text 4 here.’
list: []
},
],
我正在嘗試以這種方式實作這一目標
interface item {
text: string;
list: string[];
};
const newArr: item[] = [];
this.item.content.forEach((x, i) => {
if(x.startsWith('-') && x.endsWith(';')) {
//this is bullet list, need to add that to the previous item...
} else {
newArr.push({ text: x, list: []});
}
});
我不確定我是否以正確的方式這樣做,因為我無法想到如何訪問 newArr 中的前一個元素;任何想法如何正確地做到這一點?
uj5u.com熱心網友回復:
我會為此使用Array.reduce
interface DataItem {
text: string
list: string[]
}
function isListItem(item: string): boolean {
return item.startsWith('-') && item.endsWith(';')
}
const transformedData = data.reduce<DataItem[]>((acc, item) => {
// if item is list item
if (isListItem(item)) {
// get last DataItem from acc
const lastDataItem = acc[acc.length - 1]
// and use it as list item's parent
if (lastDataItem) {
lastDataItem.list.push(item)
} else {
console.error(`Parent text not found for list item ${item}`)
}
return acc
}
// if item is not list item, use it as new parent/DataItem
const dataItem: DataItem = {
text: item,
list: []
}
return [...acc, dataItem]
}, [])
游樂場鏈接
uj5u.com熱心網友回復:
您可以嘗試以下代碼段
let data = [
'big text 1 here.',
'big text 2 here followed by list:',
'-this is list item;',
'-this is another list item;',
'big text 3 here followed by list:',
'-this is list item;',
'-this is another list item;',
'-this is another list item;',
'big text 4 here.'
];
function format_array_to_object(data) {
let new_data = [];
let previous_data = {};
data.forEach( function( item, key ){
if ( item.trim().charAt(0) === '-' ) {
previous_data.list.push( item );
} else {
if ( Object.keys(previous_data).length !== 0 ) {
new_data.push(previous_data);
previous_data = {};
let current_elem = { text: item, list:[] };
previous_data = current_elem;
} else {
let current_elem = { text: item, list:[] };
previous_data = current_elem;
}
if ( ( key 1 ) === data.length ) {
new_data.push(previous_data);
}
}
} );
return new_data;
}
console.log(format_array_to_object(data));
uj5u.com熱心網友回復:
我會選擇 ngTemplateOutlet 選項來根據您的串列陣列生成 li 。
<ul *ngFor="let item of data">
<ng-container
*ngTemplateOutlet="
!(item.startsWith('-') && item.endsWith(';')) ? ulTemplate : bulletItem;
context: { $implicit: item }
"
>
</ng-container>
</ul>
<ng-template #ulTemplate let-item>
<li>{{ item }}</li>
</ng-template>
<ng-template #bulletItem let-item>
<ul>
<li>{{ item }}</li>
</ul >
</ng-template>
現在,通過這種方式,您已經根據您的起始字符重新定義了正確的 ng-template。此鏈接中的完整演示Stackblitz Link
uj5u.com熱心網友回復:
只是說您的字串沒有正確的引號,’并且'不一樣并且’不適用于字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/449700.html
標籤:javascript 打字稿 循环 数据结构
上一篇:比較R中多個資料幀的唯一值
