Beatrice Effy
Beatrice Effy

Reputation: 23

Array of numbers not assignable to array of strings typescript angular 2

I'm new with typescript and angular 2 and I didn't know how to solve it.

I have an array of week days and an array of seven numbers, or 0 or 1. Each '1' correspond to a week day, so Monday would be 1000000, Tuesday would be 0100000, Wednesday 0010000 and so on.

How can I say in a function that the first object of WeekDays array has to match the first object of the Frequency array and so on? I want to display the correct name, so if the number I give to it is 1000000 I have to display 'Monday', if the number is 0010100 I've to display 'Wednesday and Friday'.

If I simply equate the two arrays it gives me "Type 'string' is not assignable to type 'number'", and I cannot change the Frequency array because it arrives from a server.

@Component

weekDays: string[] = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

frequencyArray: number[] = [1, 0, 0, 0, 0, 0, 0]; //or whatever you want

loadFrequencyLabel() {
  var binary = this.prescription.frequencyArray;
  var label: string;

  for (var i = 0; i < binary.length; i++) { 
     binary[i] = this.weekDays[i];
     return label;
  }
}

Template

<p class="frequency-label">{{loadFrequencyLabel()}}</p>

THANK YOU!

Upvotes: 0

Views: 13134

Answers (2)

Heretic Monkey
Heretic Monkey

Reputation: 12113

You have frequencyArray, which you've set to a number[], and weekDays, which you set to a string[]. That's fine. I think what you want to do is make a third string[] which contains the weekdays where the frequencyArray contains a 1:

let weekDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

let frequencyArray = [1, 0, 0, 1, 0, 0];

let days = frequencyArray.reduce((acc, curr, index) => {
  if (curr === 1) acc.push(weekDays[index]);
  return acc;
}, []);
console.log(days.join(' and '));

I've simplified the code somewhat to make it more understandable. The reduce function essentially boils an array down to a single value, or in this case, to another array. The first argument is a function that is called for each element in the array (in the parameter I've named curr). It also keeps track of the result in an "accumulator" (in the parameter I've named acc). The initial value of the accumulator is passed as the second argument to reduce.

So, at the end, days consists of the days of the week where the frequencyArray contains a 1.

Then you just call the join method on the result, with " and " as the delimiter.

Note that I've used ES2015 syntax, which should work fine with TypeScript you're likely using for Angular 2.

Upvotes: 2

DGK
DGK

Reputation: 3015

Your frequency array is not an array, it's just 1 number.

@Component

weekDays: string[] = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

frequencyArray: number[] = [1, 0, 0, 0, 0, 0];

loadFrequencyLabel() {
  var binary = this.prescription.frequencyArray;
  var label: string;

  for (var i = 0; i < binary.length; i++) { 
     binary[i] = this.weekDays[i];
     return label;
  }
}

Upvotes: 0

Related Questions