{"id":9203,"date":"2017-11-29T12:12:59","date_gmt":"2017-11-29T11:12:59","guid":{"rendered":"http:\/\/www.wjst.de\/blog\/?p=9203"},"modified":"2017-11-29T12:19:39","modified_gmt":"2017-11-29T11:19:39","slug":"how-to-merge-local-and-remote-data-in-the-browser","status":"publish","type":"post","link":"https:\/\/www.wjst.de\/blog\/sciencesurf\/2017\/11\/how-to-merge-local-and-remote-data-in-the-browser\/","title":{"rendered":"How to merge local and remote data in the browser"},"content":{"rendered":"<p>In a first step <a href=\"https:\/\/threejs.org\/docs\/#manual\/introduction\/How-to-run-thing-locally\">reconfigure your browser<\/a><\/p>\n<p><strong>Safari<\/strong><br \/>\nEnable the develop menu using the preferences panel, under Advanced -&gt; &#8220;Show develop menu in menu bar&#8221;.<br \/>\nThen from the safari &#8220;Develop&#8221; menu, select &#8220;Disable local file restrictions&#8221;.<br \/>\n<strong>Chrome<\/strong><br \/>\nClose all running Chrome instances first.\u00a0Then start the Chrome executable with a command line flag:<br \/>\nchrome &#8211;allow-file-access-from-files<br \/>\nOn Mac OSX terminal you can do this with<br \/>\nopen \/Applications\/Google\\ Chrome.app &#8211;args &#8211;allow-file-access-from-files<br \/>\n<strong>Firefox<\/strong><br \/>\nIn the address bar, type\u00a0<em>about:config<\/em> Find the\u00a0<em>security.fileuri.strict_origin_policy<\/em> parameter and set it to false<\/p>\n<p>&nbsp;<\/p>\n<p>Here is the script, loosedly based on <a href=\"https:\/\/www.html5rocks.com\/en\/tutorials\/file\/dndfiles\">https:\/\/www.html5rocks.com\/en\/tutorials\/file\/dndfiles<\/a> and <a href=\"https:\/\/stackoverflow.com\/questions\/7346563\/loading-local-json-file\">https:\/\/stackoverflow.com\/questions\/7346563\/loading-local-json-file<\/a>\u00a0that loads json1 from the server, json2 from the local harddrive and merges both by id<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;input type=&quot;file&quot; id=&quot;files&quot; name=&quot;files&#x5B;]&quot; \/&gt;\r\n&lt;output id=&quot;list&quot;&gt;&lt;\/output&gt;\r\n&lt;\/html&gt;\r\n\r\n&lt;script src=&quot;jquery-1.11.1.min.js&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;script&gt;\r\n\/\/ read local comma delimited file with id and name\r\nfunction readTextFile(file) {\r\n  var rawFile = new XMLHttpRequest();\r\n  rawFile.open(&quot;GET&quot;, file, false);\r\n  rawFile.onreadystatechange = function () {\r\n    if(rawFile.readyState === 4) {\r\n      if(rawFile.status === 200 || rawFile.status == 0) {\r\n        rider = rawFile.responseText.split(&quot;\\n&quot;);\r\n        return rider;\r\n      }\r\n    }\r\n  }\r\n  rawFile.send(null);\r\n}\r\n\r\n\/\/ read server data in json format\r\nvar url = 'merge.json';\r\nvar json2;\r\n$.ajax ({\r\n    url: url,\r\n    async: false,\r\n    dataType: &quot;json&quot;,\r\n    cache: false,\r\n    success: function(data) {\r\n        json2 = data;\r\n    },\r\n    error: function(err) {\r\n        console.log(JSON.stringify(err))\r\n    }\r\n});\r\n\r\n\/\/ function to join by id\r\nfunction joinObjects() {\r\n  var idMap = {};\r\n  for(var i = 0; i &lt; arguments.length; i++) { \r\n    for(var j = 0; j &lt; arguments&#x5B;i].length; j++) {\r\n       var currentID = arguments&#x5B;i]&#x5B;j]&#x5B;'id'];\r\n       if(!idMap&#x5B;currentID]) {\r\n          idMap&#x5B;currentID] = {};\r\n        }\r\n      for(key in arguments&#x5B;i]&#x5B;j]) {\r\n          idMap&#x5B;currentID]&#x5B;key] = arguments&#x5B;i]&#x5B;j]&#x5B;key];\r\n      }\r\n    }\r\n  }\r\n  var newArray = &#x5B;];\r\n  for(property in idMap) {\r\n    newArray.push(idMap&#x5B;property]);\r\n  }\r\n  return newArray;\r\n}\r\n\r\n\/\/ selection of local fiel starts it all\r\nfunction handleFileSelect(evt) {\r\n  var files = evt.target.files; \/\/ FileList object\r\n  \/\/ files is a FileList of File objects. List some properties.\r\n  var output = &#x5B;];\r\n  for (var i = 0, f; f = files&#x5B;i]; i++) {\r\n\r\n  readTextFile( &quot;file:\/\/\/Users\/wjst\/Desktop\/&quot;+escape(f.name) );\r\n  for(var i = 0; i &lt; rider.length; i++) {\r\n    rider&#x5B;i] = rider&#x5B;i].split(&quot;,&quot;);\r\n  }\r\n  json1 = rider.map(function(x) {\r\n    return {    \r\n      &quot;id&quot;: Number(x&#x5B;0]),\r\n      &quot;name&quot;: x&#x5B;1]\r\n    }\r\n  })\r\n  console.log(json1);\r\n  console.log(json2);\r\n  console.log( JSON.stringify( joinObjects(json1, json2) ));\r\n }\r\n}\r\n\r\ndocument.getElementById('files').addEventListener('change', handleFileSelect, false);\r\n&lt;\/script&gt;\r\n<\/pre>\n\n<p>&nbsp;<\/p>\n<div class=\"bottom-note\">\n  <span class=\"mod1\">CC-BY-NC Science Surf , accessed 05.05.2026<\/span>\n <\/div>","protected":false},"excerpt":{"rendered":"<p>In a first step reconfigure your browser Safari Enable the develop menu using the preferences panel, under Advanced -&gt; &#8220;Show develop menu in menu bar&#8221;. Then from the safari &#8220;Develop&#8221; menu, select &#8220;Disable local file restrictions&#8221;. Chrome Close all running Chrome instances first.\u00a0Then start the Chrome executable with a command line flag: chrome &#8211;allow-file-access-from-files On &hellip; <a href=\"https:\/\/www.wjst.de\/blog\/sciencesurf\/2017\/11\/how-to-merge-local-and-remote-data-in-the-browser\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to merge local and remote data in the browser<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-9203","post","type-post","status-publish","format-standard","hentry","category-computer-software"],"_links":{"self":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/9203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/comments?post=9203"}],"version-history":[{"count":5,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/9203\/revisions"}],"predecessor-version":[{"id":9208,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/9203\/revisions\/9208"}],"wp:attachment":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/media?parent=9203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/categories?post=9203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/tags?post=9203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}