javascript - 具有不同 json 变量的循环显示所有项目而不是每个项目一个

标签 javascript json

所以根据我之前回答的问题(Click Here)

我用我发送到 js 变量的 php 数据创建了第二个 json 变量,所以我已经有一个显示最后显示用户阅读帖子的地方,如下所示:

 [ //defined by var = book | remove url for privacy reason.
    {
        "id": 39,
        "title": "My Pet",
        "url": "https:///novel/my-pet/",
        "chapter": {
            "id": 1192,
            "title": "35",
            "url": "https:///my-pet-35/"
        }
    },
    {
        "id": 37,
        "title": "Nobunaga’s Imouto",
        "url": "https:///novel/nobunagas-imouto/",
        "chapter": {
            "id": 1449,
            "title": "2",
            "url": "https:///nobunaga-imouto-2/"
        }
    },
    {
        "id": 2,
        "title": "Duke's Daughter",
        "url": "https:///novel/dukes-daughter/",
        "chapter": {
            "id": 1398,
            "title": "99",
            "url": "https:///dukes-daughter-99/"
        }
    }
]

第一个 json 从 cookie 中获取数据,因此用户可以跟踪他们的最后一次阅读。 至于我的第二个 json 变量显示类别中的最新帖子

[ //defined by var = newest
    {
        "id": 39,
        "title": "My Pet Chapter 35",
        "url": "https:///my-pet-35/",
    },
    {
        "id": 37,
        "title": "Nobunaga’s Imouto Chaoter 4",
        "url": "https:///nobunaga-imouto-4/",
    },
    {
        "id": 2,
        "title": "Duke's Daughter Chapter 106",
        "url": "https:///dukes-daughter-106/",
    }
]

然后我用 for 循环显示它们:

    $bookcontainer = $('#release_history'); 
    for (var i in books) {
        var book = books[i];
        var html = '<div class="row"><div class="book_history">';
        html += '<a href="'+book.url+'">'+book.title+'</a></div>';

         // Display last user read from json

        html += '<div class="newest_history">';
        for (var j in newest) { // display the newest of a post from category
            var news = newest[j];
            html += '<a href="'+news.url+'">»Chapter '+news.title+'</a></div>';
        }
        html += '</div></div></div>';
        $bookcontainer.append(html);
    }

但它会显示如下:

所以我想加上if条件,如果两个id相等。

            for (var j in newest) {
            var news = newest[j];
            if (news.id == book.id){
                html += '<a href="'+news.url+'">»Chapter '+news.title+'</a></div>';}
            }

但是,循环在显示第一个输出后停止。 有什么解决办法吗?在显示时将它们全部分开?我想显示类别的最新章节/帖子,以便用户可以知道他们上次阅读的书有新章节。

最佳答案

您正在检查这两个对象的 index id 是否相同,就像 index:0id 处的 id 一样index:3 是相同的,那么它也不会显示它,因为你没有检查它是否包含:

试试这个:

var book = [ //defined by var = book
    {
        "id": 39, //Category ID
        "title": "Last Read A",
        "url": "Chapter URL A",
    },
    {
        "id": 37, //Category ID
        "title": "Last Read B",
        "url": " Chapter URL C",
    },
    {
        "id": 2, //Category ID
        "title": "Last Read C",
        "url": " Chapter URL C",
    }
]

var book1 = [ //defined by var = newest
    {
        "id": 39, //Category ID
        "title": "Newest Chapter A",
        "url": "Post URL Chapter A",
    },
    {
        "id": 37, //Category ID
        "title": "Newest Chapter B",
        "url": " Post URL Chapter C",
    },
    {
        "id": 2, //Category ID
        "title": "Newest Chapter C",
        "url": " Post URL Chapter C",
    },
    
 //Added a different book with id 10
    {
        "id": 10, //Category ID
        "title": "Newest Chapter C",
        "url": " Post URL Chapter C",
    }
]

const bookIds = book1.map(bookData => bookData.id);

console.log('BookIDS:', bookIds)


book.forEach(bookData => {
  if(bookIds.indexOf(bookData.id) > -1){
    console.log('Matching book found with ID:', bookData.id);
  }
})

关于javascript - 具有不同 json 变量的循环显示所有项目而不是每个项目一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53957942/

相关文章:

javascript - 如何设置 ember 引擎?

javascript - 通过 Flask 发送 POST 数据

javascript - 使用网格外部的控件通过远程数据绑定(bind)在 MVC 中过滤 Kendo UI 网格

javascript - 在 JS 中解析 JSON 字符串中的特殊字符

json - 如何将对象的json转换为字典

php - 我怎样才能在mysql中制作带有类别的json?

javascript - 检测 HTML 表单上是否至少有一个字段已更改的最简单方法是什么?

javascript - 如何从方法内部的 for...in 循环获得多个返回?

json - jackson /GSON : Selecting a different representation when object has been reached via a relationship

javascript - D3 图表无法更新 -- 选择的进入和退出属性均为空