How to convert json object to array

1

I'm trying to create methods to filter but from what I understand, it needs to be an array, how do I turn it into an array and access the array's methods?

import { DataSource } from "@angular/cdk/collections";
import { EditoriasPage } from "./../models/editorias-page";
import { Editorias } from "./../models/editorias";
import { EditoriaService } from "./../services/editoria.service";
import { Component, OnInit, ViewChild } from "@angular/core";
import { MatTableDataSource, MatSort, MatPaginator } from "@angular/material";
import { merge, Observable, of as observableOf } from "rxjs";
import { catchError, map, startWith, switchMap } from "rxjs/operators";
import { SelectionModel } from "@angular/cdk/collections";

@Component({
  selector: "app-editoria-listar",
  templateUrl: "./editoria-listar.component.html",
  styleUrls: ["./editoria-listar.component.scss"]
})
export class EditoriaListarComponent implements OnInit {
  dataSource = new EditoriaDataSource(this.editoria);
  selection = new SelectionModel<EditoriaDataSource>(true, []);
  //nome das colunas
  displayedColumns = ["id", "name", "actions"];

  public editorias: Object[] | null;
  searchKey: string;

  applyFilter(filterValue: string) {
    //this.array.filter = this.searchKey.trim().toLowerCase();
  }
  onSearchClear() {
    this.searchKey = "";
    // this.applyFilter();
  }

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  constructor(private editoria: EditoriaService) {}

  ngOnInit() {
    this.editoria.getAll().subscribe(_editoria => {
      this.editorias = _editoria["result"];
      // this.loading = false;
      console.log(_editoria);
    });
  }
}

export class EditoriaDataSource extends DataSource<any> {
  constructor(private editoria: EditoriaService) {
    super();
  }
  connect(): Observable<Editorias[]> {
    return this.editoria.getAll();
  }

  disconnect() {}
}

service:

import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { HttpClient } from "@angular/common/http";

import { Editorias } from "./../models/editorias";
import { HttpUtilService } from "src/app/shared/services/http-util.service";

@Injectable({
  providedIn: "root"
})
export class EditoriaService {

  constructor(private _http: HttpClient, private _httpUtil: HttpUtilService) {}


  getAll(): Observable<Editorias[]> {
    return this._http.get<Editorias[]>(this._httpUtil.url("editorias"));
  }
}
    
asked by anonymous 02.01.2019 / 20:23

1 answer

2

Can not convert an Object to an array, and I do not think you need it. let's say you have a Person object with name and age.

For example:

{
   nome: 'Beatriz',
   idade: 16
}

In this case you can add this object to an array and then filter it.

let p1 = {nome: 'João', idade: 23};
let p2 = {nome: 'Beatriz', idade:16 };
let people = [p1,p2] //adicionando pessoas no array

//filtrando maiores de idades 

let maioresDeIdade = people.filter(p => p.idade >= 18)
console.log(maioresDeIdade) // [{nome: 'João', idade: 23}]

This way you can filter through the properties of the object.

    
02.01.2019 / 20:50