【问题标题】:Javascript: Get the array where a specific value in the array contains word (tags)Javascript:获取数组中特定值包含单词(标签)的数组
【发布时间】:2013-07-05 10:10:56
【问题描述】:

我有这个数组:

[{
  "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")
                }

            });


        });

在此处附加/解析带有动作标签的电影的标题和其他电影之后,我该怎么做?

【问题讨论】:

    标签: arrays json parsing tags


    【解决方案1】:

    不确定我是否理解您的意图。

    您可以使用item.title(在each() 循环内)访问标题,使用item.others 访问其他标题

    【讨论】:

    • 我想在所有标题后面加上标签动作。就是这样
    • 对不起,这基本上是一个错误的问题。感觉有点累。请务必将其标记为已关闭。
    【解决方案2】:

    找到后将项目推送到项目数组。

     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);
                } 
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多