[
{
"id": 1,
"description": "Take out the trash",
"completed": false
},
{
"id": 2,
"description": "Get food tonight",
"completed": false
},
{
"id": 3,
"description": "Hit the gym",
"completed": true
}
]
上面的示例陣列。按說明過濾JSON對象expressJS/UnderscoreJS
我想說只得到滿足某個查詢的對象。
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var _ = require('underscore');
app.get('/todos/query/:des', function (req, res){
var descriptionToFilter = req.params.des;
console.log(descriptionToFilter);
var filteredDesArr = _.where(todos,function(todo){
todo.description.contains(descriptionToFilter.toLowerCase());
});
res.send(filteredDesArr);
});
這是如何工作的?如何在下劃線內部輸入一個函數作爲謂詞?