0

I have a button which creates an array with an object like

[{field: field1, operator: operator1, value: 10}]

when I click on the button again it will again creates a new array like

[{field: field2, operator: operator2, value: 20}]

When I tried to push these into new array, it is becoming nested

[Array(1), Array(1)]

But I want the new array to look like

[{field: field1, operator: operator1, value: 10}, {field: field2, operator: operator2, value: 20}]

My code

public filterChange(state): void {
    this.filters.push( state.filters );
    console.log(this.filters);
}

On each click of the button 'filterChange' will call and 'state' will give the filters array generated with new filter values. Please suggest. Thanks.

vamsi
  • 1,488
  • 3
  • 28
  • 66
  • 1
    Why do the buttons create arrays with a single element in the first place? Why not just the object? –  May 19 '20 at 09:37

2 Answers2

0

Try to use spread syntax. The spread operator will take an element from array:

this.filters.push( ...state.filters );
console.log(this.filters);

An example:

let arr1 = [],
arr2 = [{
    user_id: 3, project_name: 'Project A'
}]

arr1.push(...arr2);
console.log(arr1)
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

You can use a spread syntax.

public filterChange(state): void { this.filters.push( ...state.filters ); console.log(this.filters); }