我正在用Angular2创建一个应用程序,并且我有一组派对。如何分离Typescript中的数组
const PARTIES: Party[] = [
{ id: 1, title: "Main Event", description: "The biggest, most extravagant event in the last 10,000,000 years." },
{ id: 2, title: "Secondary Event", description: "The not as biggest, less extravagant event in the last 10,000,000 years." },
{ id: 3, title: "Another Event", description: "N/A" },
{ id: 4, title: "Another Event", description: "N/A" },
{ id: 5, title: "Another Event", description: "N/A" },
{ id: 6, title: "Another Event", description: "N/A" },
{ id: 7, title: "Another Event", description: "N/A" },
{ id: 8, title: "Another Event", description: "N/A" },
{ id: 9, title: "Another Event", description: "N/A" },
{ id: 10, title: "Another Event", description: "N/A" }
];
同时保留原始阵列,我想起来拆分此阵列分成的3
段在普通的旧的JavaScript,我会使用以下内容。
var chunk_size = 3;
var arr = PARTIES;
var groups = arr.map(function(e,i){
return i%chunk_size===0 ? arr.slice(i,i+chunk_size) : null;
})
.filter(function(e){ return e; });
PARTIES = groups
但是,我正在使用TypeScript。是否有任何可能的方式来执行我想用TypeScript实现的目标?
什么让你认为你不能用TypeScript做什么你可以用JavaScript? – Marty
如果它很简单,你能告诉我一个如何实现这个功能的例子吗? @Marty –
对不起,我的意思是,如果事实上在JavaScript中工作,它将在TypeScript中完美地工作:-) – Marty