admin管理员组文章数量:1435859
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src=".1.1/jquery.min.js"></script>
<script src=".js/2.0.0/handlebars.js"></script>
</head>
<body>
<!--This is our template. -->
<!--Data will be inserted in its according place, replacing the brackets.-->
<script id="t" type="text/x-handlebars">
{{#each_when profile "gender" "male"}}
{{ID}}. {{from}} {{gender}}<br>
{{/each_when}}
</script>
<!--Your new content will be displayed in here-->
<div class="content-placeholder"></div>
<script>
var json = {
"profile": [
{ "ID": 1, "gender": "male", "from": "Olivia" },
{ "ID": 2, "gender": "male", "from": "Meagen" },
{ "ID": 3, "gender": "female, male", "from": "Aaron" },
{ "ID": 4, "gender": "female", "from": "Aaron" }
]
};
Handlebars.registerHelper('each_when', function(list, k, v, opts) {
console.log(arguments);
var i, result = '';
for(i = 0; i < list.length; ++i)
if(list[i][k] == v)
result = result + opts.fn(list[i]);
return result;
});
var t = Handlebarspile($('#t').html());
$('body').append(t(json));
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
</head>
<body>
<!--This is our template. -->
<!--Data will be inserted in its according place, replacing the brackets.-->
<script id="t" type="text/x-handlebars">
{{#each_when profile "gender" "male"}}
{{ID}}. {{from}} {{gender}}<br>
{{/each_when}}
</script>
<!--Your new content will be displayed in here-->
<div class="content-placeholder"></div>
<script>
var json = {
"profile": [
{ "ID": 1, "gender": "male", "from": "Olivia" },
{ "ID": 2, "gender": "male", "from": "Meagen" },
{ "ID": 3, "gender": "female, male", "from": "Aaron" },
{ "ID": 4, "gender": "female", "from": "Aaron" }
]
};
Handlebars.registerHelper('each_when', function(list, k, v, opts) {
console.log(arguments);
var i, result = '';
for(i = 0; i < list.length; ++i)
if(list[i][k] == v)
result = result + opts.fn(list[i]);
return result;
});
var t = Handlebars.pile($('#t').html());
$('body').append(t(json));
</script>
</body>
</html>
The result of the above code is:
1. Olivia male
2. Meagen male
But I want the following:
1. Olivia male
2. Meagen male
3. Aaron male
I am filtering an array of objects by a property and that property is a string that can contain multiple values.
Is there any one who can tell me why 3. Aaron male
is not appearing?
1 Answer
Reset to default 3The object with the ID
of 3
does not get appended to your result because your helper is filtering out objects whose gender
property is NOT equal to "male"
. Object 3's gender
is "female, male"
, so it does not pass the equality test.
If you want to check whether list[i][k]
contains v
, then you must update your logic.
var i, result = '', values;
for (i = 0; i < list.length; ++i) {
values = list[i][k].replace(/\s*,\s*/, ',').split(',');
if (values.indexOf(v) > -1) {
result += opts.fn(list[i]);
}
}
return result;
Note that I am using a regex to remove the white-space characters beside the "," before splitting - this way I can ensure that the elements in my values
array do not begin or end with a space.
This update will append the "Aaron" object to the output. However, the final result will be:
1. Olivia male<br>
2. Meagen male<br>
3. Aaron female, male<br>
If you don't want "female, male" to display for Aaron that means you don't really want to render the object's gender
property. Instead, you should just hard-code the text you want right into the template:
{{ID}}. {{from}} male<br>
本文标签: javascriptHow do I filter this array using value with handlebarsjsStack Overflow
版权声明:本文标题:javascript - How do I filter this array using value with handlebars.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745650712a2668411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论