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.

1
  • 1
    Why do the buttons create arrays with a single element in the first place? Why not just the object? Commented May 19, 2020 at 9:37

2 Answers 2

0

You can use a spread syntax.

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

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

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.