繁体   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