{"id":1819,"date":"2019-01-18T09:30:59","date_gmt":"2019-01-18T08:30:59","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=1819"},"modified":"2022-07-25T08:35:43","modified_gmt":"2022-07-25T07:35:43","slug":"javascript-get-new-changed-deleted-items-of-lists","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-get-new-changed-deleted-items-of-lists\/","title":{"rendered":"Javascript: Get New\/Changed\/Deleted items of lists"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const getNew = (oldList, newList, getId) => {\n        return newList.filter(i => !oldList.some(o => getId(o) === getId(i)));\n    };\n    const getChanged = (oldList, newList, getId, getLastModified) => {\n        return newList.filter(i => {\n            const old = oldList.filter(o => getId(o) == getId(i))[0];\n            if (old &amp;&amp; getLastModified(i) > getLastModified(old)) {\n                return true;\n            }\n        });\n    };\n    \/\/ deleted items is just the lists reversed\n    const getDeleted = (oldList, newList, getId) => {\n        return getNew(newList, oldList, getId);\n    };\n\n    const getId = (item) => {\n        return item.id;\n    };\n\n    const getLastModified = (item) => {\n        return item.lastModified;\n    };\n    \n    const dateAdd = function (date, ms) {\n        return new Date(date.getTime() + ms);\n    };\n\n    const run = () => {\n        let newDate = new Date();\n    \n        let oldList = [\n            { id: 1, lastModified: newDate },\n            { id: 2, lastModified: newDate },\n            { id: 3, lastModified: newDate },\n            { id: 4, lastModified: newDate },\n            { id: 5, lastModified: newDate },\n        ];\n        let newList = [            \n            { id: 6, lastModified: newDate },\n            { id: 2, lastModified: newDate },\n            { id: 3, lastModified: dateAdd(newDate, 10000) },\n            { id: 5, lastModified: newDate },\n            { id: 7, lastModified: newDate },\n        ];\n        \n        console.log('new', getNew(oldList, newList, getId));\n        console.log('changed', getChanged(oldList, newList, getId, getLastModified));\n        console.log('deleted', getDeleted(oldList, newList, getId));\n    };    \n    run();<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4,1],"tags":[],"class_list":["post-1819","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=1819"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1819\/revisions"}],"predecessor-version":[{"id":1828,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1819\/revisions\/1828"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=1819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=1819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=1819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}