2016-07-08 4 views
1

모델 :오브젝트의 중첩 특성을 변경

function model() { 
    return { 
     title: { 
      "content": undefined, 
      "validation": { 
       "type": "string", 
       "required": true, 
       "minLength": 1, 
       "maxLength": 3, 
       validationErrorMessage: "Your title must be a valid string between 1 and 35 characters" 
      } 
     }, 

     email: { 
      content: undefined, 
      validation: { 
       type: "email", 
       required: true, 
       minLength: 1, 
       maxLength: 60, 
       validationErrorMessage: "Your email must be between 1 and 50 characters" 
      } 
     }, 


     link: { 
      content: undefined, 
      validation: { 
       type: "url", 
       required: true, 
       minLength: 1, 
       maxLength: 500, 
       validationErrorMessage: "Your link name must be a valid email between 1 and 50 characters" 
      } 
     }, 

     description: { 
      content: undefined 
     } 
    } 
} 

Ramda 번호 :

let test = R.map(x => console.log(x.validation), model) 
console.log(test) 

로그 결과 올 : 다음

{ type: 'string', 
    required: true, 
    minLength: 1, 
    maxLength: 3, 
    validationErrorMessage: 'Your title must be a valid string between 1 and 35 characters' } 
{ type: 'email', 
    required: true, 
    minLength: 1, 
    maxLength: 60, 
    validationErrorMessage: 'Your email must be between 1 and 50 characters' } 
{ type: 'url', 
    required: true, 
    minLength: 1, 
    maxLength: 500, 
    validationErrorMessage: 'Your link name must be a valid email between 1 and 50 characters' } 
undefined 
{ title: undefined, 
    email: undefined, 
    link: undefined, 
    description: undefined } 

않는 이유 :

let test = R.map(x => x.validation = "replacement test", model) 
    console.log(test) 

로그 :

내가 기대 x.validation의 콘텐츠를 교체해야 할 것
{ title: 'replacement test', 
    email: 'replacement test', 
    link: 'replacement test', 
    description: 'replacement test' } 

, 전체가 아닌 x 값. 나는 그것을 얻지 못한다.

+0

제목 개체가 다른 모든 개체와 다른 이유는 무엇입니까? –

+0

'linkmodel'을'model'으로 참조합니까? – Andru

+0

예, 죄송합니다. 수정되었습니다. –

답변

2

map은 모든 Ramda 함수와 마찬가지로 불변의 방식으로 사용하기위한 것입니다. 함수에서 반환 한 요소가 포함 된 목록을 반환합니다. 그러나 당신은 (의도적으로) 당신의 기능에서 아무것도 반환하지 않습니다, 그냥 원래의 가치를 조정합니다. Javascript는 ES6 화살표 기능의 멋진 기능 중 하나 인 과제 표현에서 복귀하고 있습니다. 당신이 그대로 개체를 유지하지만, '검증'속성을 업데이트하려면

,이 할 수 있습니다

R.map(R.assoc('validation', 'replacement text'), model()); 

assoc는 개체의 단순 복제, 주어진 값으로 지정된 속성을 덮어 쓰기한다.

1

그래서, 당신의 replacer lambda를 살펴 봅시다.

x => x.validation = "replacement test" 

x.validation = "replacement test"에서 x으로 매핑하십시오. 당신이

x => 1 

을 할 경우는, 그 배열의 모든 값은 1

당신이지도를 사용하는 경우, 그것은 원래의 배열을 변경됩니다. 이 경우, x은 이되는 x.validation이됩니다.

당신은 각 반복에 뭔가를 적용하고이 경우

R.foreach(x => x.validation = "replacement test"); 
0

에 대해 forEach를 사용, 그래서 당신은 x.validation에 할당한다는 사실은 무관하다.

중요한 것은 매번 반환되는 것과 그로 인해 발생하는 일입니다. 본질적으로 문자열을 반환하고 매번 결과를 할당합니다.

관련 문제