简体   繁体   English

在JavaScript中的2个JSON数组之间查找匹配的JSON项,并将CSS应用于UL列表中的匹配项

[英]Find matching JSON items between 2 JSON arrays in JavaScript and apply CSS to matching items in a UL list

Using JavaScript I need to take a JSON array of Tag items and generate an HTML UL list of all tags from that JSON data. 使用JavaScript,我需要获取一个Tag项目的JSON数组,并从该JSON数据生成所有标签的HTML UL列表。

Next I have a 2nd JSON data-set of Tags in which I need to look in the first Tag JSON data and find each matching item between the 2 sets of tags. 接下来,我有一个标签的第二个JSON数据集,我需要在其中查找第一个标签JSON数据,并找到两组标签之间的每个匹配项。

When a Tag in the 2ns JSON data-set exist in the 1st Tag JSON data-set I need to add a CSS class to the UL list I generated from the 1st Tag data on each matching Tag 当第一个标签JSON数据集中存在2ns JSON数据集中的标签时,我需要向每个匹配标签上的第一个标签数据生成的UL列表中添加CSS类

1st JSON Tag Data-set 第一个JSON标记数据集

var allTagsJson = [{
    "id": "1",
    "name": "Tag 1"
}, {
    "id": "2",
    "name": "Tag 2"
}, {
    "id": "3",
    "name": "Tag 3"
}, {
    "id": "4",
    "name": "Tag 4"
}, {
    "id": "5",
    "name": "Tag 5"
}];

2nd JSON Tag Data-set 第二个JSON标记数据集

    "id": "1",
    "name": "Tag 1"
}, {
    "id": "4",
    "name": "Tag 4"
}, {
    "id": "5",
    "name": "Tag 5"
}];

So in this sample data my UL list would have : 因此,在此样本数据中,我的UL列表将具有:

  • Tag 1 标签1
  • Tag 2 标签2
  • Tag 3 标签3
  • Tag 4 标签4
  • Tag 5 标签5

As the 2nd JSON data-set has tags 1, 4, and 5. The list above would need to add the CSS class active to tags 1, 4, and 5 作为第二JSON数据集具有标签1,图4和5上面的列表将需要的CSS类添加active到标签1,图4和5

JSFiddle to play with: https://jsfiddle.net/jasondavis/tm9fsyvb/ 要使用的JSFiddle: https ://jsfiddle.net/jasondavis/tm9fsyvb/

var listTagsJson = [{

// generate 2 UL lists from JSON Data
$(function() {
    var allTagsHtml = '';

    // this list needs to add CSS class active to each item that has a matching tag in the 2nd list of tags
    $.each(allTagsJson, function(index, val) {
        console.log(val.name);
        allTagsHtml += " <li><a href='#" + val.name + "'>" + val.name + "</a></li>";
        $('#all-tags').html(allTagsHtml);
    });

    var listTagsHtml = '';
    $.each(listTagsJson, function(index, val) {
        console.log(val.name);
        listTagsHtml += " <li><a href='#" + val.name + "'>" + val.name + "</a></li>";
        $('#list-tags').html(listTagsHtml);
    });

});

Assuming the ID's are only property that need comparing .... start with second array and while iterating it store each id as key in a hashmap object then check if it exists while iterating the first array and add class accordingly 假设ID只是需要比较的属性..从第二个数组开始,并在迭代时将每个ID作为键存储在哈希映射对象中,然后在迭代第一个数组时检查它是否存在,并相应地添加类

$(function() {
  // id object
  var listIds = {}

  var listTagsHtml = '';
  $.each(listTagsJson, function(index, val) {
    // add id to object
    listIds[val.id] = true;
    listTagsHtml += " <li><a href='#" + val.name + "'>" + val.name + "</a></li>";
  });

  var allTagsHtml = '';
  $.each(allTagsJson, function(index, val) { 
    // apply class based on id object
    var className = listIds[val.id] ? 'match' : '';
    allTagsHtml += " <li class='" + className + "'><a href='#" + val.name + "'>" + val.name + "</a></li>";
  });

  // note these don't belong inside the loops when using html string concatenation
  $('#all-tags').html(allTagsHtml);
  $('#list-tags').html(listTagsHtml);

});

This approach requires no additional dom searching or array filtering and is very efficient 这种方法不需要额外的dom搜索或数组过滤,并且非常有效

DEMO 演示

Add this code to the second loop: 将此代码添加到第二个循环中:

$("#all-tags").find("li").each(function(){
       if($(this).find("a")[0].innerHTML == val.name){
            $(this).addClass("active");
       }
});

You can try with a custom function to filter the matched elements form first array. 您可以尝试使用自定义函数来过滤第一个数组中匹配的元素。 If the element matches then add a class. 如果元素匹配,则添加一个类。

This snippet may be useful 该代码段可能有用

//Rest of code
var listTagsHtml = '';
    $.each(listTagsJson, function(index, val) {
        // A custom function to check for the matched element
        // if it matches it will return a string(a custom class)
        var myClass=matchJson(val.id)
        // adding that class to li

        listTagsHtml += " <li class='"+myClass+"'><a href='#" + val.name + "'>" + val.name + "</a></li>";
        $('#list-tags').html(listTagsHtml);
    });



    //a function to check if it matches
    function matchJson(id){
        // using array filter function
        var filteredArray = allTagsJson.filter(function(a,b){
        return a.id ===id;
      })
      var toReturn = ''
      if(filteredArray !== undefined){
        toReturn ='customClass';
      }
      return toReturn;
    }
    });

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM