Exploring new features in Angular has led me to encounter a challenge involving Objects and Arrays. Imagine having a customized component called Filter
, along with a service named FilterService
. Below is the code snippet:
filter.component.ts
import { Component, OnInit } from '@angular/core';
import { FilterService } from '../services/sinsense-filter.service';
@Component({
...
})
export class FilterComponent implements OnInit {
public appliedFilters: string[] = []
constructor(public filterService: SisenseFilterService) {}
ngOnInit() {
var obj = this.filterService.filtersApplied;
var result = Object.keys(obj).map(function (key) {
return [Number(key), obj[key]];
// return [obj[key];
});
// this.appliedFilters=result;
// console.log(this.appliedFilters);
// console.log(typeof(result));
}
}
Note: I've tried potential solutions which are currently commented out.
Here's the corresponding service code in filter-service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class FilterService {
filtersApplied: object;
constructor() {
this.filtersApplied = new Object();
this.filtersApplied={
property1: "value1",
property2: "value2",
property3: "value3"
}
}
}
While referencing an article on GeeksForGeeks: How to convert an Object {} to an Array [] of key-value pairs in JavaScript?
However, a compatibility error arises:
Type 'any[][]' is not assignable to type 'string[]'.
Type 'any[]' is not assignable to type 'string'.
I seek guidance on achieving my desired appliedFilters
array structure, devoid of keys:
["value1", "value2", "value3"]