我有一个类似下面的对象数组,
{
"data": [
{
"name": "HTML",
"description": "Hyper Text Markup Language"
},
{
"name": "CSS",
"description": "Cascading Style Sheet"
},
{
"name": "JS",
"description": "Javascript"
}
]
}
我得到上面的对象数组作为来自这个终点/ get / technologies的响应,假设这个终点是否会有一个查询字符串,比如/ get / technologies?q = CSS如何过滤响应只是为了渲染下面,
{
"data": [
{
"name": "CSS",
"description": "Cascading Style Sheet"
}
]
}
我有一个节点/快递app所以在控制器中如果我执行“req.query.q”然后我可以获取查询参数,使用该查询参数我如何过滤原始对象数组..我遇到了一些npm包但不确定哪个会满足我的需求,
https://www.npmjs.com/package/filter-array
https://www.npmjs.com/package/object-filter
https://www.npmjs.com/package/array-filter
https://www.npmjs.com/package/array-query
如果我可以抓取查询参数并找到匹配的文本也很好..例如,如果查询参数只是“SS”,则结果应该呈现CSS和JS,因为文本“S”是两者都有。
答案 0 :(得分:1)
试试这个:
var d = [
{
"name": "HTML",
"description": "Hyper Text Markup Language"
},
{
"name": "CSS",
"description": "Cascading Style Sheet"
},
{
"name": "JS",
"description": "Javascript"
}
];
var a = d.filter(function(el) {
return el.name === 'CSS';
});
答案 1 :(得分:1)
您可以使用filter
:
var data = [
// the array to be filtered
];
var filteredArray = data.filter(item => (item.name === req.query.q));