1

I'm creating an application with Angular2, and I have an array of parties.

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" }
];

While preserving the original array, I would like to split this array up into segments of 3.

In plain old JavaScript, I would use the following.

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

However, I'm using TypeScript. Is there any possible way to perform what I'm trying to achieve using TypeScript?

6
  • 1
    What makes you think you can't do with TypeScript what you could with JavaScript? Commented Jul 27, 2016 at 4:05
  • Could you show me an example of how to implement this functionality if it's so easy? @Marty Commented Jul 27, 2016 at 4:06
  • Sorry, what I meant was if something was in fact working in JavaScript, it would work perfectly fine in TypeScript as well :-) Commented Jul 27, 2016 at 4:08
  • That's understandable, but this code breaks my application @Marty Commented Jul 27, 2016 at 4:08
  • What errors do you get? As Basarat said, that original code isn't valid anyway which would break your application if it were plain JS as well. Commented Jul 27, 2016 at 4:09

1 Answer 1

5

Your JavaScript code:

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

Is not correct. If it was it would be valid TypeScript and it would just work because JavaScript is TypeScript https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html :)

Fix

The following is a working sample :

const PARTIES = [
  { 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" }
];

var chunk_size = 3;
const groups = PARTIES.map(function(e,i){
    return i%chunk_size===0 ? PARTIES.slice(i,i+chunk_size) : null;
})
.filter(x=>!!x)

console.log(groups);

Some notes on the fixes:

Sign up to request clarification or add additional context in comments.

2 Comments

I'm doing it within a class, would that effect it?
Thanks, man. I was trying to do it from within a class. @basarat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.