다시 말해; 어떻게 변환 할 수 있습니까?
// an array of length >= 3
let myArray = returnOfSomeParametricFunction(); // assuming repeating rhs removes dryness
let myObj = { staticKeyName: myArray[1], anotherStaticKeyName: myArray[2] };
단일 라이너에. 아마도 다음과 같습니다.
let myObj = returnOfSomeParametricFunction().reduce(arr=> { staticKeyName: arr[1], anotherStaticKeyName: arr[2] };
이 경우 한 줄로해야하고 같은 범위에 새 변수를 도입 myObj
하지 않고 가독성에 신경 쓰지 않는다면 다음과 같은 화살표 함수를 사용합니다.
let myObj = (a => ({ staticKeyName: a[1], anotherStaticKeyName: a[2] }))(
returnOfSomeParametricFunction());
myObj
올바른 유형의 속성이 있는지 확인할 수 있습니다 . 예를 들어, 주어진
declare function returnOfSomeParametricFunction(): [Date, number, string];
그런 다음 myObj
유형이 있습니다.
/*
let myObj: {
staticKeyName: number;
anotherStaticKeyName: string;
}
*/
좋아요, 도움이 되길 바랍니다. 행운을 빕니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다