Javascript:获取数组中特定值包含单词(标记)的数组

时间:2013-07-05 10:10:56

标签: arrays json parsing tags

我有这个数组:

[{
  "title": "Pirates of the Caribbean 5",
  "others": "lorem",
  "tags": "action,experimental,comedy"
},
{
  "title": "Toy Story 3",
  "others": "lorem",
  "tags": "animation,family,comedy"
},
{
  "title": "Pirates of the Caribbean 1",
  "others": "lorem",
  "tags": "action,adventure,comedy"
}]

我正在尝试获取标记为 action 的所有详细信息(标题,其他)。

要查找字符串是否包含操作,请执行以下操作:

$.getJSON('jsonpath.json', function(data) {
            var items = [];

            $.each(data, function(count,item) {
                var a = $.inArray("action", item.tags.split(","));

                if (a != -1){
                    console.log("Found")
                }

            });


        });

在这里附加/解析标题和其他用动作标记的电影后我该怎么办?

2 个答案:

答案 0 :(得分:0)

不确定我理解你的意图。

您可以使用item.title(在每个()循环内)和其他item.others

访问标题

答案 1 :(得分:0)

一旦找到,就将项目推送到items数组。

 var items = [];
 $.getJSON('jsonpath.json', function(data) {
        $.each(data, function(count,item) {
            var a = $.inArray("action", item.tags.split(","));

            if (a != -1){
                console.log("Found");
                items.push(item);
            } 
        });
    });