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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

Working 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