为什么以下函数在执行时返回undefined? 好像过滤器函数确实返回了相应的对象,所以我不明白为什么它不能传递给回调函数。我是JS的新手。
$query = "SELECT * FROM ORDER BY `bv_subcategory`";
$temp = row['bv_subCategory'];
<div>
//in loop
if($temp != row['bv_subcategory'])
{
echo "</div><div>"
$temp = $row['bv_subcategory'];
}
//after loop
</div>
答案 0 :(得分:0)
如果您有一个对象数组,并且其中一个或多个对象的ID属性已分配给'15a',那么您的过滤器应该可以正常工作。
以下是一个例子:
function getUserById(usersArr, userId, cb) {
cb(usersArr.filter(function (el) {
return el.id === userId;
})[0]);
}
var users = [{
id: '15a',
name: 'bob',
email: 'bob@example.com',
address: '123 main street'
}, {
id: 'zxv',
name: 'jim',
email: 'jim@example.com',
address: '234 main ave'
}];
getUserById(users, '15a', function (user) {
document.getElementById('output').innerHTML = 'The user with the id 15a has the email of ' + user.email + ' the name of ' + user.name + ' and the address of ' + user.address;
});
jsfiddle:https://jsfiddle.net/0skhn9k9/2/
编辑:要使您的示例正常工作,您需要返回回调值
function getUserById(usersArr, userId, cb){
return cb((usersArr.filter(function(el){
return el.id === userId;
})[0]));
}
getUserById(users, '15a', function(user){
return 'The user with the id 15a has the email of ' + user.email + ' the name of ' + user.name + ' and the address of ' + user.address;
});
var users = [
{
id: '43d',
email: 'james@gmail.com',
name: 'james',
address: '16 N'
},
{
id: '15a',
email: 'carry@gmail.com',
name: 'Carry',
address: '14 N'
},
{
id: '87t',
email: 'jeff@gmail.com',
name: 'Jeff',
address: '23 N'
},
];