最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I filter this array using value with handlebars.js? - Stack Overflow

programmeradmin1浏览0评论

<!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?

Share Improve this question edited Jan 25, 2017 at 4:39 76484 9,0033 gold badges21 silver badges32 bronze badges asked Jan 22, 2017 at 12:24 Rehaan KkhaanRehaan Kkhaan 811 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The 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>
发布评论

评论列表(0)

  1. 暂无评论