gpt4 book ai didi

javascript - 如何仅返回具有目标参数的对象?

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:46 25 4
gpt4 key购买 nike

例如,我有这个 POST 方法:

request_salaries(type)

类型可以是红色蓝色绿色

函数返回此对象:

{
data: [
{
id: 1,
name: "Linda",
type: "Red"
},
{
id: 2,
name: "Mark",
type: "Green
},
{
id: 3,
name: "Susan",
type: "Blue"
}
]
}

现在,我只需要返回 request_salaries(type) 中传递的数据 sat。

例如,如果我请求request_salaries(Red)

它应该返回:

{
data: [
{
id: 1,
name: "Linda",
type: "Red"
},
]
}

如何实现这一目标?

谢谢。

最佳答案

只需过滤掉正确的值:

const response = { /* your data */ };
const type = 'red';

const filteredResponse = response.data.filter(item => item.type === type)

关于javascript - 如何仅返回具有目标参数的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51748218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com