2016-08-31 1 views
3

reddit 주석 데이터 구조를 평평하게하기 위해 normalise api를 사용하려고합니다.reddit 주석 데이터 구조를 병합하는 방법은 무엇입니까?

예를 들어, 아래 전화 :

https://www.reddit.com/r/reactjs/comments/506gca/what_backend_and_db_are_you_using_with_react/.json 

당신에게 중첩 된 구조를 제공한다. 다음과 같습니다

[{ 
      "kind": "t1", 
      "data": { 
      "subreddit_id": "t5_2zldd", 
      "banned_by": null, 
      "removal_reason": null, 
      "link_id": "t3_506gca", 
      "likes": null, 
      "replies": { 
       "kind": "Listing", 
       "data": { 
       "modhash": "", 
       "children": [ 
        { 
        "kind": "t1", 
        "data": { 
         "subreddit_id": "t5_2zldd", 
         "banned_by": null, 
         "removal_reason": null, 
         "link_id": "t3_506gca", 
         "likes": null, 
         "replies": { 
         "kind": "Listing", 
         "data": { 
          "modhash": "", 
          "children": [ 
          { 
           "kind": "t1", 
           "data": { 
           "subreddit_id": "t5_2zldd", 
           "banned_by": null, 
           "removal_reason": null, 
           "link_id": "t3_506gca", 
           "likes": null, 
           "replies": { 
            "kind": "Listing", 
            "data": { 
            "modhash": "", 
            "children": [ 
             { 
             "kind": "t1", 
             "data": { 
              "subreddit_id": "t5_2zldd", 
              "banned_by": null, 
              "removal_reason": null, 
              "link_id": "t3_506gca", 
              "likes": null, 
              "replies": "", 
              "user_reports": [], 
              "saved": false, 
              "id": "d724wpm", 
              "gilded": 0, 
              "archived": false, 
              "report_reasons": null, 
              "author": "bbennett36", 
              "parent_id": "t1_d724qxh", 
              "score": 7, 
              "approved_by": null, 
              "controversiality": 0, 
              "body": "I don't think everyone here is only doing front-end lol", 
              "edited": false, 
              "author_flair_css_class": null, 
              "downs": 0, 
              "body_html": "<div class=\"md\"><p>I don't think everyone here is only doing front-end lol</p>\n</div>", 
              "subreddit": "reactjs", 
              "name": "t1_d724wpm", 
              "score_hidden": false, 
              "stickied": false, 
              "created": 1472554709, 
              "author_flair_text": null, 
              "created_utc": 1472525909, 
              "distinguished": null, 
              "mod_reports": [], 
              "num_reports": null, 
              "ups": 7 
             } 
             } 
            ], 
            "after": null, 
            "before": null 
            } 
           }, 
           "user_reports": [], 
           "saved": false, 
           "id": "d724qxh", 
           "gilded": 0, 
           "archived": false, 
           "report_reasons": null, 
           "author": "nicholasxuu", 
           "parent_id": "t1_d71oyfk", 
           "score": -10, 
           "approved_by": null, 
           "controversiality": 0, 
           "body": "why even bother asking on this sub? asking front-end experts what backend stack they like.", 
           "edited": false, 
           "author_flair_css_class": null, 
           "downs": 0, 
           "body_html": "<div class=\"md\"><p>why even bother asking on this sub? asking front-end experts what backend stack they like.</p>\n</div>", 
           "subreddit": "reactjs", 
           "name": "t1_d724qxh", 
           "score_hidden": false, 
           "stickied": false, 
           "created": 1472554467, 
           "author_flair_text": null, 
           "created_utc": 1472525667, 
           "distinguished": null, 
           "mod_reports": [], 
           "num_reports": null, 
           "ups": -10 
           } 
          } 
          ], 
          "after": null, 
          "before": null 
         } 
         }, 
         "user_reports": [], 
         "saved": false, 
         "id": "d71oyfk", 
         "gilded": 0, 
         "archived": false, 
         "report_reasons": null, 
         "author": "bbennett36", 
         "parent_id": "t1_d71nyho", 
         "score": 5, 
         "approved_by": null, 
         "controversiality": 0, 
         "body": "I just want to see what stacks people are using out there.", 
         "edited": false, 
         "author_flair_css_class": null, 
         "downs": 0, 
         "body_html": "<div class=\"md\"><p>I just want to see what stacks people are using out there.</p>\n</div>", 
         "subreddit": "reactjs", 
         "name": "t1_d71oyfk", 
         "score_hidden": false, 
         "stickied": false, 
         "created": 1472531897, 
         "author_flair_text": null, 
         "created_utc": 1472503097, 
         "distinguished": null, 
         "mod_reports": [], 
         "num_reports": null, 
         "ups": 5 
        } 
        }, 
        { 
        "kind": "t1", 
        "data": { 
         "subreddit_id": "t5_2zldd", 
         "banned_by": null, 
         "removal_reason": null, 
         "link_id": "t3_506gca", 
         "likes": null, 
         "replies": "", 
         "user_reports": [], 
         "saved": false, 
         "id": "d720vlw", 
         "gilded": 0, 
         "archived": false, 
         "report_reasons": null, 
         "author": "zorlan", 
         "parent_id": "t1_d71nyho", 
         "score": 1, 
         "approved_by": null, 
         "controversiality": 0, 
         "body": "This. The database should be so far removed that it doesn't affect React one iota. I would even go so far as to say this post is off topic. ", 
         "edited": false, 
         "author_flair_css_class": null, 
         "downs": 0, 
         "body_html": "<div class=\"md\"><p>This. The database should be so far removed that it doesn't affect React one iota. I would even go so far as to say this post is off topic. </p>\n</div>", 
         "subreddit": "reactjs", 
         "name": "t1_d720vlw", 
         "score_hidden": false, 
         "stickied": false, 
         "created": 1472548717, 
         "author_flair_text": null, 
         "created_utc": 1472519917, 
         "distinguished": null, 
         "mod_reports": [], 
         "num_reports": null, 
         "ups": 1 
        } 
        } 
       ], 
       "after": null, 
       "before": null 
       } 
      }, 
      "user_reports": [], 
      "saved": false, 
      "id": "d71nyho", 
      "gilded": 0, 
      "archived": false, 
      "report_reasons": null, 
      "author": "shanestillwell", 
      "parent_id": "t3_506gca", 
      "score": 6, 
      "approved_by": null, 
      "controversiality": 0, 
      "body": "Why does it matter? React should be a few layers removed from any database and it's quite agnostic about the backend. Heck, React itself doesn't even know how to talk to the backend. ", 
      "edited": false, 
      "author_flair_css_class": null, 
      "downs": 0, 
      "body_html": "<div class=\"md\"><p>Why does it matter? React should be a few layers removed from any database and it's quite agnostic about the backend. Heck, React itself doesn't even know how to talk to the backend. </p>\n</div>", 
      "subreddit": "reactjs", 
      "name": "t1_d71nyho", 
      "score_hidden": false, 
      "stickied": false, 
      "created": 1472530637, 
      "author_flair_text": null, 
      "created_utc": 1472501837, 
      "distinguished": null, 
      "mod_reports": [], 
      "num_reports": null, 
      "ups": 6 
      } 
     }, {...},{...}] 

정확하게, data.children 배열은 다시 등 replies을 가지고 할 수있는, 다시 data을 가질 수 replies 키를 가지고있다.

이 재귀 구조를 어떻게 평평하게하는지보고 싶습니다. replies을 업데이트하는 동안, 감속기에서 구조를 올바르게 탐색하기가 어려워졌습니다. (그것을 표시하는 동안, 그것으로 쉽게 탐색 할 수 있습니다)

어떤 도움이 될 것입니다!

+2

우리에게 입력의 구조와 원하는 출력에 대한 간단한 예를 보여 유용 할 것이다. 이미 구조를 평평하게하려는 시도를 했습니까? 그렇다면 포함 시키십시오. –

+0

@TomFenech : 시도했지만 제대로 작동하지 못해서 스키마를 제대로 얻지 못했습니다. 같은 구조가 다시 나에게 돌아왔다! – batman

+0

문제는 당신이 원하는 것을 명시 적으로 정의 할 때까지 사람들이 당신을 도울 수 없다는 것입니다. –

답변

2

저는 reddit API에 익숙하지 않지만이 점에 대해 어떻게 생각하십니까? 귀하의 예제가 x에 저장되어있는 경우

// this should be handled more explicitly 
function normalize(input) { 
    if(input.data) return normalize(input.data); 
    if(input.children) return normalize(input.children); 
    return input; 
} 

function build(input) { 
    const result = {}; 
    if(input.replies) { 
    input.replies = normalize(input.replies).map((data) => { 
     const reply = normalize(data); 
     Object.assign(result, build(reply)); 
     return reply.id; 
    }); 
    } 
    result[ input.id ] = input; 
    return result; 
} 

, build(normalize(x[0])); 당신을 제공해야합니다 :

{ 
    "d724wpm": { 
    // ... 
    "replies": "", 
    // ... 
    "id": "d724wpm", 
    // ... 
    "body": "I don't think everyone here is only doing front-end lol" 
    // ... 
    }, 
    "d724qxh": { 
    // ... 
    "replies": [ 
     "d724wpm" 
    ], 
    // ... 
    "id": "d724qxh", 
    // ... 
    "body": "why even bother asking on this sub? asking front-end experts what backend stack they like." 
    // ... 
    }, 
    "d71oyfk": { 
    // ... 
    "replies": [ 
     "d724qxh" 
    ], 
    // ... 
    "id": "d71oyfk", 
    // ... 
    "body": "I just want to see what stacks people are using out there." 
    // ... 
    }, 
    "d720vlw": { 
    // ... 
    "replies": "", 
    // ... 
    "id": "d720vlw", 
    // ... 
    "body": "This. The database should be so far removed that it doesn't affect React one iota. I would even go so far as to say this post is off topic. " 
    // ... 
    }, 
    "d71nyho": { 
    // ... 
    "replies": [ 
     "d71oyfk", 
     "d720vlw" 
    ], 
    // ... 
    "id": "d71nyho", 
    // ... 
    "body": "Why does it matter? React should be a few layers removed from any database and it's quite agnostic about the backend. Heck, React itself doesn't even know how to talk to the backend. " 
    // ... 
    } 
} 
+0

당신이 당신의 자신의'정상적인 '방법으로 대답 해준 것이 대단합니다! – batman

관련 문제