In backend PHP I have this defined in loop:
foreach ( $data as $res ){
$approved[ ] = [ 'id' => $count, 'title' => "some title" ];
$count++;$title=...
)
and then final list is created:
$list[ ] = [ 'list' => 'approved', 'values' => $approved ];
return ['list' => $list ];
In javascript I am getting these results via ajax call:
this.myoutput = {};
this.http.get('backend_url).then(data => {
let result = this.helper.isJson( data.response ) || [ ];
console.log(result);
this.myoutput.approved = result.list.find( item => item.list === 'approved' );
console.log(this.myoutput.approved);
})
The first console.log(result);
gives me all data something like:
list: (1) […]
0: {…}
list: "approved"
values: (13) […]
0: Object { id: "1", title: "aaa" }
1: Object { id: "2", title: "bbb" }
2: Object { id: "3", title: "ccc" }
but console.log(this.myoutput.approved);
gives me undefined. Is something what I am doing wrong here?
My Answer:
$res['id'], 'name' => $res['name'], 'status' => $res['status'] ];}_In frontend JavaScript, you can use the `find` method to search for a specific object in the array of objects based on a specific property value. Here's an example of how you can do this:
javascript
// Assume 'approved' is the array of objects received from the backend
const foundObject = approved.find(obj => obj.id === 'desired_id');
if (foundObject) {
console.log('Found object:', foundObject);
} else {
console.log('Object not found');
}
In this example, the `find` method is used to search for an object in the `approved` array where the `id` property matches the desired ID value. If the object is found, it will be stored in the `foundObject` variable, and you can then access its properties or perform any other operations as needed.
Rate this post
5 of 5 based on 5298 votesComments