ES6를 지원하지 않는 Google 시트 앱 스크립트에서 큰 데이터 모음을 정렬하려고합니다! (나는 생각한다)
배열은 다음과 같습니다. var data = [[ "Frank", "[email protected]", 21], [ "Mark", "[email protected]", 23]]
나는 배열을 객체로 바꾸는 아이디어가 마음에 들었습니다. 예를 들어 이름으로 모든 객체에 액세스 할 수 있습니다.
Frank.name
Frank.email
Frank.age
나는 다음을 시도했다
function Driver(name, email, age) {
this.name= name;
this.email= email;
this.age = age;
}
그리고
for(var i=0; i<data.length; i++){
data[i][0]= new Driver (data[i][0],data[i][1],data[i][2])
}
하지만 작동하지 않습니다. 이것으로 나를 도와 줄 수 있습니까?
귀하의 요구 사항을 이해한다면
이름으로 모든 개체에 액세스 할 수있는 개체로 배열을 바꾸는 아이디어를 좋아합니다.
올바르게, 솔루션은 다음과 같이 간단합니다.
ES6 버전
function Driver(name, email, age) {
this.name= name;
this.email= email;
this.age = age;
}
const data = [["Frank","franksmail",2], ["name2", "mail2", 3], ["name3", "mail3", 4]]
const mappedData = data
//map the array of arrays to an array of Drivers
.map(props => new Driver(...props))
//reduce the array of drivers to a single object
.reduce((accumulator, currentDriver) => {
accumulator[currentDriver.name] = currentDriver
return accumulator
}, {})
console.log(mappedData)
console.log(mappedData.Frank.email)
ES5 버전 :
function Driver(name, email, age) {
this.name= name;
this.email= email;
this.age = age;
}
var data = [["Frank","franksmail",2], ["name2", "mail2", 3], ["name3", "mail3", 4]]
var mappedData = data
//map the array of arrays to an array of Drivers
.map(function (props) {
return new Driver(props[0], props[1], props[2])
})
//reduce the array of drivers to a single object
.reduce(function (accumulator, currentDriver) {
accumulator[currentDriver.name] = currentDriver
return accumulator
}, {})
console.log(mappedData)
console.log(mappedData.Frank.email)
경고 :
같은과 다이버에있는 경우 name
귀하의 data
배열 다음 중 하나를 잃어 버릴 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다