admin管理员组文章数量:1410697
I have an object array which look something like below,
{
"data": [
{
"name": "HTML",
"description": "Hyper Text Markup Language"
},
{
"name": "CSS",
"description": "Cascading Style Sheet"
},
{
"name": "JS",
"description": "Javascript"
}
]
}
I get the above object array as a response from this end point /get/technologies, suppose if this end point is gonna have a query string some thing like this /get/technologies?q=CSS how can i filter the response just to render the below,
{
"data": [
{
"name": "CSS",
"description": "Cascading Style Sheet"
}
]
}
I have a node/express app so in the controller if i do "req.query.q" then i can grab the query parameter, with that query parameter how can i filter the original object array.. i came across some npm packages but not sure which would suite my need,
It would also be nice if i can grab the query parameter and find the matching texts.. say for example if the query parameter is just "SS" then the result should render both CSS and JS since the text "S" is there in both of them.
I have an object array which look something like below,
{
"data": [
{
"name": "HTML",
"description": "Hyper Text Markup Language"
},
{
"name": "CSS",
"description": "Cascading Style Sheet"
},
{
"name": "JS",
"description": "Javascript"
}
]
}
I get the above object array as a response from this end point /get/technologies, suppose if this end point is gonna have a query string some thing like this /get/technologies?q=CSS how can i filter the response just to render the below,
{
"data": [
{
"name": "CSS",
"description": "Cascading Style Sheet"
}
]
}
I have a node/express app so in the controller if i do "req.query.q" then i can grab the query parameter, with that query parameter how can i filter the original object array.. i came across some npm packages but not sure which would suite my need,
https://www.npmjs./package/filter-array
https://www.npmjs./package/object-filter
https://www.npmjs./package/array-filter
https://www.npmjs./package/array-query
It would also be nice if i can grab the query parameter and find the matching texts.. say for example if the query parameter is just "SS" then the result should render both CSS and JS since the text "S" is there in both of them.
Share Improve this question asked Apr 26, 2016 at 21:16 SaiSai 2,0426 gold badges34 silver badges56 bronze badges2 Answers
Reset to default 3Working Example
Try this:
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';
});
You could just use filter
:
var data = [
// the array to be filtered
];
var filteredArray = data.filter(item => (item.name === req.query.q));
本文标签: javascriptFilter object array via nodejsStack Overflow
版权声明:本文标题:javascript - Filter object array via nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745036292a2638805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论