簡體   English   中英

如何從 JSON.parse(xhr.response) 獲取內容

[英]How to get content from JSON.parse(xhr.response)

我向客戶端發送 GET 請求,主體中的對象返回給我。 現在我需要處理一個特定的對象,例如,在 ID 3 下顯示對象的名稱。我該怎么做?

const requestURL = '/coffee/all'

const xhr = new XMLHttpRequest();

xhr.open('GET', requestURL)

xhr.onload = () => {

    var datas = JSON.parse(xhr.response);

    console.log(datas)
}

xhr.send()

在此處輸入圖片說明

const requestURL = '/coffee/all'
const xhr = new XMLHttpRequest();

xhr.open('GET', requestURL)

xhr.onload = () => {

    var datas = JSON.parse(xhr.response);

    console.log(datas.find(item => item.id === 3).name)
}

xhr.send()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM