0

I have the following empty array declared:

  myYears: { year: number, checked: boolean }[] = [];

My goal is to fill this array of objects with a year and a boolean on whether or not that year is checked. I like having the property names here as well. When initially populating myYears, I fill it like below to obtain my list of unique years from myBigDataList object array. This is successful in giving me an array of unique year numbers, and I've gotten this far. Now what's the best way to turn this into an array of {year: number, checked: boolean} objects instead of just numbers, and where everything is initialized to true?

  this.myYears= Array.from(new Set(this.myBigDataList.map(item => {item.activityYear})));

1 Answer 1

1

You can do it in your map function.

let myYears = [
  1971,
  1972,
  1973
];

myYears = myYears.map(year => {
  return {
    year: year,
    checked: true
  };
});

console.log(myYears);

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

2 Comments

Actually, this doesn't quite work as now it's not returning a unique array by year. If I add in an extra 1971 for example, I only want one of them returned.
Ah. Then do the map after you make the unique array. Map 'this.myYears' and have each element of the array return an object.

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.