前提
Google Bloggerを利用。投稿ページに関連記事を表示するため、特定のラベルが付いた投稿のデータを抽出加工しようと思っています。
特定のラベルが付いた投稿のデータを、コールバック関数でJSONデータとして受け取り、一部をオブジェクトとしてpushをつかって配列に追加しました
実現したいこと
格納したデータを取り出して重複処理をしたい
(配列に追加したオブジェクトのうち重複しているオブジェクトを削除したい)
現状
配列への追加が上手くいっていないのか、配列からデータを取り出すことができませんでした。(Array(0)
となってしまう)
いろいろ調べましたが解決の糸口が見つかりません。
解決策をご教示いただけると大変助かります。
補足
重複処理の方法に特にこだわりはないのですが、
重複しているオブジェクトの削除は【Javascript】オブジェクトの配列をuniqueにする(重複を削除する)ときはfilterではなくMapオブジェクトを使う - Qiitaを利用しようと思いました。
該当のソースコード
javascript
1<script> 2const currentPost = "<data:post.url/>"; // いま開いているページ 3//<![CDATA[ 4 5let unit = []; // この変数にデータを格納したい 6 7function feedData(json) { 8 // コールバック関数を受け取る関数 9 for (let i = 0; i < json.feed.entry.length; i++) { 10 let jsonFeedEntry = json.feed.entry[i]; 11 12 for (let k = 0; k < jsonFeedEntry.link.length; k++) { 13 if (jsonFeedEntry.link[k].href != currentPost) { 14 let imageUrl = 15 "media$thumbnail" in jsonFeedEntry 16 ? jsonFeedEntry.media$thumbnail.url 17 : "NoImage"; 18 let unitObject = {}; 19 20 if (jsonFeedEntry.link[k].rel == "alternate") { 21 unitObject.title = jsonFeedEntry.title.$t; 22 unitObject.link = jsonFeedEntry.link[k].href; 23 unitObject.image = imageUrl; 24 25 unit.push(unitObject); 26 } 27 } 28 } 29 } 30} 31 32console.log(unit); 33 34const uniqUnit = Array.from( 35 new Map(unit.map((data) => [data.link, data])).values() // 回答をもとに修正 36); 37console.log(uniqUnit); // Array(0)となってしまう 38 39//]]> 40</script>
コールバック関数
<b:loop>
はラベルの数だけループするBloggerのタグです
javascript
1<b:loop values='data:post.labels' var='label'> 2 3<script async='async' expr:src='"/feeds/posts/summary/-/" + data:label.name + "?alt=json-in-script&callback=feedData&max-results=10"' type='text/javascript'/> 4 5</b:loop>
JSON
JSONデータのサンプルです
json
1{ 2 "version": "1.0", 3 "encoding": "UTF-8", 4 "feed": { 5 "xmlns": "http://www.w3.org/2005/Atom", 6 "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/", 7 "xmlns$blogger": "http://schemas.google.com/blogger/2008", 8 "xmlns$georss": "http://www.georss.org/georss", 9 "xmlns$gd": "http://schemas.google.com/g/2005", 10 "xmlns$thr": "http://purl.org/syndication/thread/1.0", 11 "title": { 12 "type": "text", 13 "$t": "" 14 }, 15 "subtitle": { 16 "type": "html", 17 "$t": "" 18 }, 19 "link": [ 20 { 21 "rel": "http://schemas.google.com/g/2005#feed", 22 "type": "application/atom+xml", 23 "href": "https://sample.com/feeds/posts/summary" 24 }, 25 { 26 "rel": "alternate", 27 "type": "text/html", 28 "href": "https://sample.com/search/label/apple" 29 } 30 ], 31 "entry": [ 32 { 33 "title": { 34 "type": "text", 35 "$t": "appleとbanana" 36 }, 37 "summary": { 38 "type": "text", 39 "$t": "" 40 }, 41 "link": [ 42 { 43 "rel": "replies", 44 "type": "application/atom+xml", 45 "href": "https://sample.com/feeds/21/comments/default", 46 "title": "" 47 }, 48 { 49 "rel": "alternate", 50 "type": "text/html", 51 "href": "https://sample.com/2022/03/apple-and-banana.html", 52 "title": "appleとbanana" 53 } 54 ], 55 "media$thumbnail": { 56 "xmlns$media": "http://search.yahoo.com/mrss/", 57 "url": "https://....jpg", 58 "height": "72", 59 "width": "72" 60 }, 61 "thr$total": { 62 "$t": "0" 63 } 64 }, 65 { 66 "title": { 67 "type": "text", 68 "$t": "apple" 69 }, 70 "summary": { 71 "type": "text", 72 "$t": "" 73 }, 74 "link": [ 75 { 76 "rel": "replies", 77 "type": "application/atom+xml", 78 "href": "https://sample.com/feeds/61/comments/default", 79 "title": "" 80 }, 81 { 82 "rel": "alternate", 83 "type": "text/html", 84 "href": "https://sample.com/2022/01/apple.html", 85 "title": "apple" 86 } 87 ], 88 "media$thumbnail": { 89 "xmlns$media": "http://search.yahoo.com/mrss/", 90 "url": "https://....jpg", 91 "height": "72", 92 "width": "72" 93 }, 94 "thr$total": { 95 "$t": "0" 96 } 97 }, 98 { 99 "title": { 100 "type": "text", 101 "$t": "appleとbanana" 102 }, 103 "summary": { 104 "type": "text", 105 "$t": "" 106 }, 107 "link": [ 108 { 109 "rel": "replies", 110 "type": "application/atom+xml", 111 "href": "https://sample.com/feeds/45/comments/default", 112 "title": "" 113 }, 114 { 115 "rel": "alternate", 116 "type": "text/html", 117 "href": "https://sample.com/2022/02/apple-and-banana.html", 118 "title": "appleとbanana" 119 } 120 ], 121 "media$thumbnail": { 122 "xmlns$media": "http://search.yahoo.com/mrss/", 123 "url": "https://....jpg", 124 "height": "72", 125 "width": "72" 126 }, 127 "thr$total": { 128 "$t": "0" 129 } 130 } 131 ] 132 } 133} 134
発生している問題・エラーメッセージ
変数unit
にデータが格納されていない?のか、uniqUnit
がArray(0)
となります
デベロッパーツールのコンソールをみると
オブジェクトを受け取った配列unit
は
はじめは
Array(4)
となり、展開すると
Array(4) 0: {title: 'appleとbanana', link: 'https:...apple-banana.html', image: 'https:...jpg'} 1: {title: 'apple', link: 'https:...apple.html', image: 'https:...jpg'} 2: {title: 'appleとbanana', link: 'https:...apple-banana.html', image: 'https:...jpg'} 3: {title: 'banana', link: 'https:...banana.html', image: 'https:...jpg'} length: 4
ブラウザをリロードをすると[]
が表示され展開すると下記のようになります。
[] 0: {title: 'appleとbanana', link: 'https:...apple-banana.html', image: 'https:...jpg'} 1: {title: 'apple', link: 'https:...apple.html', image: 'https:...jpg'} 2: {title: 'appleとbanana', link: 'https:...apple-banana.html', image: 'https:...jpg'} 3: {title: 'banana', link: 'https:...banana.html', image: 'https:...jpg'} length: 4
uniqUnit
については、はじめは
Array(0)
となり、展開すると
length: 0
ブラウザをリロードをすると[]
が表示され展開するとlength: 0
となります。
試したこと
こちらの状況→関数外で宣言し、関数内で要素をpushした配列が空の配列としてみなされる - スタック・オーバーフローと似ているような気がするので非同期が関係しているのかも?
と考えPromiseなどを自分なりに書いてみましたが、上手くいきませんでした。
回答1件
あなたの回答
tips
プレビュー