2017-11-14 12 views
0

내 API에서 JSON이 반환됩니다.normalizr 새 속성 및 그룹 지정

[{ 
     "_id": "id1", 
     "userId": "u1", 
     "createdOn": "2017-11-13T19:24:05.269Z" 
    }, { 
     "_id": "id2", 
     "userId": "u2", 
     "createdOn": "2017-11-13T19:23:59.777Z" 
    }, { 
     "_id": "id3", 
     "userId": "u3", 
     "createdOn": "2017-11-09T17:34:14.507Z" 
    }, { 
     "_id": "id4", 
     "userId": "u4", 
     "createdOn": "2017-11-08T18:15:54.303Z" 
    } 
] 

먼저 나는 createdOn 분할 (createdOn.split 같은 ("T"를) [0]) 및 날짜에만 문자열을 포함하는 새 속성 createdDate을 설정해야합니다. 그런 다음 createdDate라는 새 속성을 기준으로 분류하고 같은 날짜에 생성 된 항목으로 그룹화하려고합니다. 그래서 저는 다음과 같은 구조를 얻으려고합니다.

{ 
    "history": { 
     "2017-11-08": { 
      "id4": { 
       "_id": "id4", 
       "userId": "u4", 
       "createdOn": "2017-11-08T18:15:54.303Z", 
       "createdDate": "2017-11-08" 
      } 
     }, 
     "2017-11-09": { 
      "id3": { 
       "_id": "id3", 
       "userId": "u3", 
       "createdOn": "2017-11-09T17:34:14.507Z", 
       "createdDate": "2017-11-09" 
      } 
     }, 
     "2017-11-13": { 
      "id1": { 
       "_id": "id1", 
       "userId": "u1", 
       "createdOn": "2017-11-13T19:24:05.269Z", 
       "createdDate": "2017-11-13" 
      }, 
      "id2": { 
       "_id": "id2", 
       "userId": "u2", 
       "createdOn": "2017-11-13T19:23:59.777Z", 
       "createdDate": "2017-11-13" 
      } 
     } 
    } 
} 

는 또한 여러 스키마

 export const historySchema = new schema.Entity(
     "history", 
     {}, 
     { idAttribute: "_id" }, 
    ); 

    export const historyGroupingSchema = new schema.Entity(
     "history", 
     { el: [historySchema] }, 
     { idAttribute: "createdDate" }, 
    ); 

normalize(history, [ 
     historyGroupingSchema, 
    ]) 

(spliting 날짜 시간 문자열) 'processStrategy'와 다른 옵션을 시도했지만 작동하지 않습니다. normalizr으로이를 수행하는 방법.

답변

0

각 개체를 반복해야하고 만든 날짜 (두 번째 질문)를 구문 분석해야하는 만든 날짜의 바로 날짜 부분을 기준으로 개체를 분류하려면 날짜 만 반환하는 함수를 만들었습니다. 당신은 당신이 다음 루프 배열을 할 수있는 날짜를 구문 분석하고 같은 날 내가 얻기 위해 노력하고

var p=[{ 
 
     "_id": "id1", 
 
     "userId": "u1", 
 
     "createdOn": "2017-11-13T19:24:05.269Z" 
 
    }, { 
 
     "_id": "id2", 
 
     "userId": "u2", 
 
     "createdOn": "2017-11-13T19:23:59.777Z" 
 
    }, { 
 
     "_id": "id3", 
 
     "userId": "u3", 
 
     "createdOn": "2017-11-09T17:34:14.507Z" 
 
    }, { 
 
     "_id": "id4", 
 
     "userId": "u4", 
 
     "createdOn": "2017-11-08T18:15:54.303Z" 
 
    } 
 
]; 
 

 
    function getDatepart(createdOn) 
 
    { 
 
    var dateFieldValue= new Date(createdOn) 
 
    var year = dateFieldValue.getFullYear()+""; 
 
    var month = (dateFieldValue.getMonth()+1)+""; 
 
    var day = dateFieldValue.getDate()+""; 
 
    var dateFormat = year + "-" + month + "-" + day;//use your format 
 
    console.log(dateFormat) 
 
    return dateFormat; 
 
    
 
    } 
 
    getDatepart(p[0].createdOn)

+0

을 가진 개체를 가져되면 momentjs 특별히 사용할 수있는 다른 좋은 JS 라이브러리가 형식을 변경할 수 있습니다 이것은 normalizr [https://github.com/paularmstrong/normalizr]에 의해 수행, 내가 언급 한 날짜 구성 요소를 얻으려면 나를 위해 충분하다 ("T"로 분할하고 첫 번째 요소를 취한다) – Edayan