0

I have this code which works as expected:

this.filteredCampaigns = this.allCampaigns.filter((item) => item.status!.includes(filterConditions))
//this.filteredCampaigns is of type Campaign[] | null and allallCampaigns is of type 
 product.Campaign[]

the.filteredCampaingns is empty, while the allCampaigns stores all data. When I try to concat it, it returns undefined and no compiler errors:

this.filteredCampaigns.concat(this.allCampaigns.filter((item) => item.status!.includes(filterConditions)))  //returns undefined

When I instead try push, I get an error

Type 'Campaign[]' has no properties in common with type 'Campaign'.ts(2559)

this.filteredCampaigns.push(this.allCampaigns.filter((item) => 
 item.status!.includes(filterConditions)))  //returns undefined

How to solve this? I can only declare the value of the array, but I need to add/push more data in to it which does not work.

In the following order:

this.filteredCampaigns = this.allCampaigns.filter((item) => item.description?.toLowerCase().includes(filteredString))

 this.filteredCampaigns.concat(this.allCampaigns.filter((item) => item.type?.toLowerCase().includes(filteredString)));
4
  • It's not clear what order you're calling this code in. What is the value of this.filteredCampaigns before you call .concat() or .push()? Commented Mar 28, 2021 at 0:10
  • Updated my post. No combination of .concat() or .push() works. Initially I declared the value, and then .concat() (returned undefined). Since it didnt work I tried .push() which game me compilations error instead. If I instead declare it with the same value I tried to push/concat it will work. Commented Mar 28, 2021 at 0:14
  • Shouldn't you spread your Array when used as an argument for push()? Commented Mar 28, 2021 at 0:19
  • Also, concat doesn't alter the arrays it's being called on, nor the arguments provided - it returns a shallow copy. Are you storing the result of that call anywhere? Commented Mar 28, 2021 at 0:23

1 Answer 1

2

You're getting the error on push() because you're attempting to push an Array as a single element onto the other Array, which Typescript is rightfully calling out. You should use push() like this instead:

this.filteredCampaigns.push(...this.allCampaigns.filter((item) => 
 item.status!.includes(filterConditions)))

Using the spread syntax means that you're pushing the separate elements of that Array onto the target Array.

As far as the concat() problem, it's because you're not assigning the result of the concat() operation to anything. concat() does not operate on the Array it's called on (it doesn't operate "in place") but returns a value (MDN). If you want to use concat(), assign the value to the target Array:

this.filteredCampaigns = this.filteredCampaigns.concat(this.allCampaigns.filter((item) => item.type?.toLowerCase().includes(filteredString)));
Sign up to request clarification or add additional context in comments.

Comments

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.