перебрать массив в JavaScript
Я передаю на сервер массив вот таким кодом
public function showMyPosts(){
$author_id = auth()->user()->getAuthIdentifier();
$MyPosts = Posts::where('author_id', $author_id)->get();
$MyPosts->toJson();
return response()->json($MyPosts);
}
вот js код
$(document).ready(function(){
let token = document.querySelector('input[name="_token"]').value
let formData = new FormData();
let name = document.getElementById("userName");
let lastname = document.getElementById("userLastname");
formData.append('_token', token);
fetch('/showMyPosts', {
method: "post",
body: formData
}).then(function (response){ return response.json()})
.then(function (result){
console.log(result)
})
});
Вот это отображается в консоли
[
{
"id": 1,
"created_at": "2023-07-06T10:33:47.000000Z",
"updated_at": "2023-07-06T10:33:47.000000Z",
"content": "ioppjijpjji",
"author_id": 1,
"img": null
},
{
"id": 2,
"created_at": "2023-07-10T18:37:32.000000Z",
"updated_at": "2023-07-10T18:37:32.000000Z",
"content": "hbhjknjjjjk",
"author_id": 1,
"img": "assets/images/post/men-in-black-movie-agents-wallpaper-1920x1200_6.jpg.jpg"
},
{
"id": 4,
"created_at": "2023-07-10T18:53:11.000000Z",
"updated_at": "2023-07-10T18:53:11.000000Z",
"content": "rtfuydftgfhfghff",
"author_id": 1,
"img": "assets/images/post/underfined.png.png"
},
{
"id": 5,
"created_at": "2023-07-12T13:48:23.000000Z",
"updated_at": "2023-07-12T13:48:23.000000Z",
"content": "Hello",
"author_id": 1,
"img": "assets/images/post/s3M4W65qIzEenEPuXCZb5DlW3WjcMhlED6TVanEO.jpg;"
},
{
"id": 6,
"created_at": "2023-07-12T14:25:55.000000Z",
"updated_at": "2023-07-12T14:25:55.000000Z",
"content": "eeeawaew",
"author_id": 1,
"img": "assets/images/post/DeZrjw1gI8Mv7MhiHgPdHwen7GNsjDdh4joPo8fW.jpg;"
},
{
"id": 8,
"created_at": "2023-07-12T14:46:20.000000Z",
"updated_at": "2023-07-12T14:46:20.000000Z",
"content": "sadwdada",
"author_id": 1,
"img": "assets/images/post/dFl3TxRsdMyK1uOE7AM8eKYrafG9NRb16p9ZAFGa.jpg; "
},
{
"id": 9,
"created_at": "2023-07-12T14:55:20.000000Z",
"updated_at": "2023-07-12T14:55:20.000000Z",
"content": "e2qeq2eq2",
"author_id": 1,
"img": "assets/images/post/oNQxTT9q3mI03KGZPLdBDE9VkzCoIjafy6tUOHma.jpg;,,assets/images/post/JKb6QAaYwOv8BSQZTQfL7g65OftvIq4FuMMnOuWH.jpg;,,assets/images/post/qx4tHYikjY4KTIDXwjiRwsy5NCEqmF7j2mt5qlQV.png;,,assets/images/post/UQcW2OJslr7QqyQmtdbtgvQXRXsw8WHsK0tuIGCk.png;,,assets/images/post/j3kMIDHyCSaeLPOGt0oTLUIXpeHF3j72NLgwPfy1.jpg;,"
},
{
"id": 10,
"created_at": "2023-07-12T14:56:56.000000Z",
"updated_at": "2023-07-12T14:56:56.000000Z",
"content": "3rw3rw3rw",
"author_id": 1,
"img": "assets/images/post/Mhyf5i1DtNpoCKvr1HFyg7R3ukPfByGz5ZPCj9Cq.jpg;assets/images/post/zGnOLZ4BUQBsmj7qt1FkOWnOaurslf1oPfUVrsQA.jpg;assets/images/post/aPA5jwAaLIyacuyOxJ0sDdJ4LS2CjLxJ6jXjY2E3.png;assets/images/post/FTvuJd0MxUEFPtLab1Ontc510AAcVefNbDm4tElJ.png;assets/images/post/kVXUUV5WVHPm0AjyRk3N02xHBlL6BadYNQfcz4KK.jpg"
},
{
"id": 11,
"created_at": "2023-07-12T15:00:19.000000Z",
"updated_at": "2023-07-12T15:00:19.000000Z",
"content": "rewrasw",
"author_id": 1,
"img": "assets/images/post/5kQJT7Yt0xL9FWDpWBmXJxtR6iTax3gLQKyCbOY6.jpg; assets/images/post/ZHxzo7zfJRuwxAFCU9NumagmVCTu4DC2CnSy5dQx.jpg; assets/images/post/n6RF6mjzOcC1VuKv62lw5PmIAc95k2Tw5kCaOYeC.png; assets/images/post/B0jFugWATE8sslTEfjMFbZggCPtO5cD2n8zk1tA9.png; assets/images/post/hPy7nQiXGfMij9VioWxqA3QzsGhWt4soTeNSTP8q.jpg"
},
{
"id": 12,
"created_at": "2023-07-12T15:41:29.000000Z",
"updated_at": "2023-07-12T15:41:29.000000Z",
"content": "sdfsfs",
"author_id": 1,
"img": "assets/images/post/SPCC6G7YtbsdlhwhgVVCPXnYzyr2mFvb9UbrKNza.jpg; assets/images/post/6zlSefGruSymV9ladaiGZsDt2ngi4pirjItMpVra.jpg; assets/images/post/MLas7oH8vXNZL3A1omrhXu773TNqTKdxkaMTQ7sh.png; assets/images/post/GPAY9TbLUKjqX0fMgdyfC7ag7DB9k7mteGGLGolH.png; assets/images/post/oKhZNBUo5TLSvrb7vqpzJifdtAxyjZX8JUqcE9xE.jpg"
},
{
"id": 14,
"created_at": "2023-07-12T15:53:28.000000Z",
"updated_at": "2023-07-12T15:53:28.000000Z",
"content": "efsfdsfsefssefgg",
"author_id": 1,
"img": "assets/images/post/lW7mMsMkYBYHLmfUbN8mXOypCT72bBMn98d5QdhP.jpg; assets/images/post/yG8uzJckBCEM4ihS0X6mf2YddMO7FAaPYG9AGbEg.jpg; assets/images/post/XpV5Wtq7T6uzgt86oWk4FBwcd5KtqLbG47N4D0ei.jpg; assets/images/post/EmZ4myeyI0UFQhzTk6aR0PCQmaNDF3x4zTWaN6Hc.jpg"
},
{
"id": 15,
"created_at": "2023-07-13T06:39:14.000000Z",
"updated_at": "2023-07-13T06:39:14.000000Z",
"content": "uygytyryufgty",
"author_id": 1,
"img": "assets/images/post/auK8nuQB98qOxSQ8aFTFBO8qVQVQSaJLP762HXlw.jpg; assets/images/post/xgu2qW8qitH1zwzsuK0Y9gvWIMdLW270Tad5P9Sl.jpg; assets/images/post/W0FRaytVzVq8jzRTjfFNjJM23uxISmXR0np7JDnF.jpg; assets/images/post/FBU8INRkvzSAsg9McTSDubKobmJmvPNUbW7f7Y96.jpg"
}
]
Подскажите пожалуйста, как можно перебрать этот массив , чтобы получить сами значения , и потом отобразить их на странице
Источник: Stack Overflow на русском