You can implement it with .map and Object.keys()
const result = data.map(item => Object.keys(item)[0]);
console.log(result); // ['name1', 'name2']
If you want to perform that method to your http call. You can do so by:
this.http
.get(this.URL1)
.pipe(map(res => res.map(item => Object.keys(item)[0])))
.subscribe((res: Response) => {...});
Answer from KShewengger on Stack OverflowYou can implement it with .map and Object.keys()
const result = data.map(item => Object.keys(item)[0]);
console.log(result); // ['name1', 'name2']
If you want to perform that method to your http call. You can do so by:
this.http
.get(this.URL1)
.pipe(map(res => res.map(item => Object.keys(item)[0])))
.subscribe((res: Response) => {...});
Try this !
for(let i = 0; i < this.data.length; i++ )
{
for(let key of this.data[i])
{
if(this.data[i][key].hasOwnProperty("name"))
ids.push(this.data[i][key][name]);
}
}
As your object lies inside another object with key as name.
How to iterate to an array of json objects within the template using *ngFor directive
angularjs - Is there a better way to iterate over this JSON data structure? - Stack Overflow
How to Iterate over angular json object
Problem iterating through JSON in Angular
Looks like you need to iterate through datebases.databases object. As soon as $scope.databases is:
{
databases: [],
totalSize: ..,
..
}
you need the following ng-repeat on your page:
<ul>
<li ng-repeat="contact in databases.databases">
{{ contact.name }}
</li>
</ul>
As Andrew said try contact.name in your html code.
"contact" on its own would be the entire object. you have to specify what part of the object you want to use.
The JSON you're retrieving from the service doesn't return an array but an object, on your component you should do this:
this.employeeService.getEmployees().subscribe((data) => {
this.emplist = data.data;
});
Since your service is returning an object, your *ngFor directive can't iterate through it.
Try with this changes. The problem is that You are converting the JSON to type which is not with the same structure.
extract interface Response {
status: string;
data: Employee2[];
}
getEmployees(): Observable<Response> {
return this.http //.get(this.url)
.get<Response>(this.url)
.pipe(catchError(this.handleError<Response>('getEmployees', {})));
}
export class NewemployeeComponent implements OnInit {
emplist: Employee2[];
// Inject the service into newemployees.component.ts file via a constructor.
constructor(private employeeService: EmployeeService) {}
ngOnInit(): void {
this.employeeService.getEmployees().subscribe((response) => {
this.emplist = response.data;
});
}
}
For Angular 6.1+ , you can use default pipe keyvalue ( Do review also ) :
<ul>
<li *ngFor="let recipient of map | keyvalue">
{{recipient.key}} --> {{recipient.value}}
</li>
</ul>
WORKING DEMO
Previously : As you are saying :
Angular2 - *ngFor / loop through json object with array , this couldn't help you
You dont have any need of that, coz your code works fine , please check
WORKING DEMO
Your code (the part you shown) works fine (see the plunker linked below).
As the only thing not shown in your question is the way you assign your Javascript Object to the variable persons, I urge you to show you more code / search the issue there.
https://plnkr.co/edit/rj4K2sKHTHsVtdt4OWfC?p=preview
@Component({
selector: 'my-app',
template: `
<div>
<div *ngFor="let person of persons">
<p>{{person.name}}</p> <!-- this works fine-->
<p *ngFor="let color of person.colors"> <!--I want to list the colors of the person here-->
{{color.name}}
</p>
</div>
</div>
`,
})
export class App {
name:string;
constructor() {
}
persons = [
{
"name": "Mike",
"colors": [
{"name": "blue"},
{"name": "white"}
]
},
{
"name": "Phoebe",
"colors": [
{"name": "red"},
{"name": "yellow"}
]
}
];
}
Nothing complicated, it’s just that your syntax is wrong.
The for loop needs to be written like this:
for(var i=0; i < user.User.Stats.length; i++)
I.e. without the superfluous $, without the superfluous ;, and also there is no data inside Stats.
See http://jsfiddle.net/4kzzy/176/
Also note you could use angular.forEach instead.
I'm not sure whether it is suitable solution for your problem, but if you want to display only active users, try this design:
<div ng-controller="MyCtrl">
<div ng-repeat="user in _users | filter:{User.active:1}">
{{user.User.userid}}
</div>
</div>
Show: 19571
http://jsfiddle.net/Jd2Hw/