例如,我有这个 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/