How to convert object to array by javascript / angular

the extracted api format is pure object format

{"name":"james","city":"Taipei","country":"Taiwan"}

using ngFor to run the table cannot run out because object format is not supported

how do I change it to array format?

want to become

[
    {"name":"james","city":"Taipei","country":"Taiwan"}
]

Dec.15,2021

although data can be processed directly in Component or Directive, a more orthodox approach would be to implement a pipe , such as

import { PipeTransform, Pipe } from '@angular/core';

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
  transform(value, args:string[]) : any {
    let keys = [];
    for (let key in value) {
      keys.push(key);
    }
    return keys;
  }
}

after

*ngFor="let key of obj | keys"

similarly, you can also implement the tuple format of [key, value]

.

[] .push ({"name": "james", "city": "Taipei", "country": "Taiwan"}) is not OK?


let o = {"name":"james","city":"Taipei","country":"Taiwan"}
let arr = [o]

what's the point of turning this array into such an array?

if you want to traverse an object, you can use for. In. or use Object.keys () and Object.values ()

in es6 .
Menu