在html()之前替换()并添加“&”两个字符串之间

时间:2014-05-25 05:04:36

标签: javascript jquery json replace

请查看 Fiddle Example 。它搜索包含“葡萄糖胺”的字符串。如何去除“葡萄糖胺”并添加“&”如果它返回两个字符串,如下所示:

A Item
Sulfate
B Item
Sulfate & HCl

我在.replace("Glucosamine","")之后使用append收到了未定义的错误。

JSON:

[{"title":"A","Ingredient":"Glucosamine Sulfate,Vitamin C"},{"title":"B","Ingredient":"Vitamin D,Glucosamine Sulfate,Glucosamine HCl,Vitamin A"}]

代码:

$.ajax({
    url: "text.json",
    success: function (data) {

        $(data.query.results.json.json).each(function (index, item) {        
            var title = item.title;
            var ingredients = item.Ingredient; 
              ingredients = ingredients.split(",");
           $.each(ingredients,function(i,ingredient){
            if (ingredient.indexOf("Glucosamine") >= 0) {
            $('.' + title+'glu').append('<h5>'+ingredient+'</h5>') 
             }
            });
        });
    },
    error: function () {}
});

HTML:

<h3>A Item</h3>
<div class="Aglu"></div>
<h3>B Item</h3>
<div class="Bglu"></div>

2 个答案:

答案 0 :(得分:2)

答案

问题是你正在尝试(据我所知)在jQuery对象上使用replace,如下所示:

// this will not work
$('.' + title+'glu').append('<h5>'+ingredient+'</h5>').replace("Glucosamine","");

问题是replace()是javascript中String对象的函数,jQuery对象中没有replace方法。您要做的是针对replace()变量运行ingredient

// this will work
$('.' + title+'glu').append('<h5>'+ingredient.replace("Glucosamine","")+'</h5>');

不回答

但是,根据您的最新评论,我认为这不会对您有所帮助。虽然它与您遇到的实际问题无关,但我会继续并快速说明我将如何处理您实际尝试的问题。我会这样写你的函数:

    $(data.query.results.json.json).each(function (index, item) {        
        var title = item.title;
        var ingredients = item.Ingredient; 

        // this is good.  I  like the use of split here
        ingredients = ingredients.split(",");

        // here I would just filter the array.  Don't bother with indexOf.
        // You can just as easily use regex.  I've chosen to use an
        // actual regex pattern but you can also use something like this
        // just as easily: ingredient.match("Glucosamine");.  I just
        // chose to use regex for the sake of using i for case insensi-
        // tivity.  glucosamineBased is now an array of only the glucose
        // ingredients
        var glucosamineBased = ingredients.filter(function(ingredient){
            return ingredient.match(/glucosamine\s/i);
        });

        // now that we know which ones are actually glucose based, we
        // don't need to itterate through them.  Instead we can just jump
        // on combining them.  I use join which works the opposite as
        // split above.  After they are joined into one big happy string,
        // I strip out the glucosamine words.  Easy-peasy.  Just keep in
        // mind that you need g for global (don't just replace the first
        // one, but replace all instances of the pattern) and maybe i for
        // case insensitivity.
        $('.' + title+'glu').append('<h5>' +glucosamineBased.join(' & ').replace(/glucosamine\s/gi, '')+'</h5>');
    });

希望这有帮助。

演示

http://jsfiddle.net/HANvQ/

(哎呀......忘了演示)

答案 1 :(得分:1)

如果数组包含多个单词&#34;氨基葡萄糖&#34;的实例,那么添加&符号会更加棘手,但以下应该可以解决问题:

$(data.query.results.json.json).each(function (index, item) {
    var title = item.title;
    var ingredients = item.Ingredient;
    ingredients = ingredients.split(",");
    var string = '';
    $.each(ingredients, function (i, ingredient) {
        if (ingredient.indexOf("Glucosamine") >= 0) {
            ingredient = ingredient.replace("Glucosamine","");
            string += (string.length == 0) ? ingredient : " & "+ingredient;
        }
    });
    $('.' + title + 'glu').append('<h5>' + string + '</h5>')
});

http://jsfiddle.net/wDyZd/2/