Defuzer
Defuzer

Reputation: 119

Array assigned to variable is 'undefined'

I got multiple checkboxes to select variants.

And I am getting checked data from like this:

get violCategoriesFormArraySelectedIds(): string[] {
    return this.violCategories
      .filter((cat, catIdx) => this.violCategoriesFormArr.controls.some((control, controlIdx) => catIdx === controlIdx && control.value))
      .map(cat => cat.value);     
  }

then console.log output is:

console.log('arrViol: '+this.violCategoriesFormArraySelectedIds);

arrViol: Unauthorized access to information, Getting confidential information by supposedly trustworthy person (phishing)

then I am trying to assign it to my varaible like:

this.violation = this.violCategoriesFormArraySelectedIds;

but got error

Type 'string[]' is not assignable to type 'string'

this is my variable. I tried also violation: string;

violation = '';

I tried also

this.violation = this.violCategoriesFormArraySelectedIds.toString();

and it "works" console.log is good but now..

I want assign this.violation to my interface (violation?: any; (I just tried any but nothing))

 answers: Answers = {

    id: undefined,
    name: '',
    date: '',
    time: '',
    violation: this.violation,
    description: '',
    numofpeople: 0,

  };

but this.violation despite the correct output in console.log is

undefined

when I am trying to do console.log(this.answers.violation)

I do not need put into my MySql an array output. I want just string separated by commas.

Upvotes: 0

Views: 71

Answers (1)

Josh Wulf
Josh Wulf

Reputation: 4877

You are looking for this:

this.violation = this.violCategoriesFormArraySelectedIds.join(', ')

This will convert an array of strings into a string with the elements of the array separated by commas.

Then you need to assign it to this.answers:

this.answers = {
    id: undefined,
    name: '',
    date: '',
    time: '',
    violation: this.violation,
    description: '',
    numofpeople: 0,
  };

console.log(this.answers)

Upvotes: 1

Related Questions