일부 데이터가 포함 된 개체가 있습니다. 특정 키를 선택하고 두 키가 일치하면 키와 값도 생략하고 싶습니다. 내가하고 싶은 무엇Ramda는 키와 값이 일치 할 경우 생략합니다.
const obj = {
title: 'some title',
description: 'some descrption',
image: 'default_image.png'
}
, description
및 image
을 추출하고 'default_image.png'
의 값이있는 경우 다음 image
를 생략하는 것입니다 : 여기 내 개체입니다.
const fn = R.compose(
// if image === 'default_image.png' then omit it
R.pickAll(['description', 'image'])
)
위의 두 번째 부분에 가장 적합한 ramda 기능이 무엇인지 확실하지 않습니다. 당신은 두 개의 서로 다른 기능과 내가 생각할 수 ramda의 ifElse
const obj1 = {
title: 'some title',
description: 'some descrption',
image: 'default_image.png'
}
const obj2 = {
title: 'title',
description: 'descrption',
image: 'image.png'
}
const withImage = R.pickAll(['description', 'image']);
const withoutImage = R.pickAll(['description']);
const hasDefault = obj => obj['image'] == 'default_image.png'
const omit = R.ifElse(hasDefault, withoutImage, withImage);
console.log(omit(obj1));
console.log(omit(obj2));
가장 간단한 방법으로 적용 분야를 점검 한 부울 함수를 만들 수 있습니다 사용
환상적인 대답, 스캇 감사합니다! – gosseti