How to convert a json object to array

Asked

Viewed 356 times

1

I’m trying to create methods to filter but from what I understand, it needs to be an array, as I do to transform into array and have access to array 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"));
  }
}

1 answer

2

It is not possible to convert an Object into an array, and I don’t think you need it. say you have an object Person with name and age.

for example:

{
   nome: 'Beatriz',
   idade: 16
}

in this case you can add this object in an array and then filter.

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 by the properties of the object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.