1

I'm trying to sort a big collection of data in google sheets app script which doesn't support ES6! (I think)

The array looks something like this var data = [["Frank", "[email protected]", 21],["Mark", "[email protected]", 23]]

I would love the idea to turn the array into objects where I can access every object by the name ex:

Frank.name
Frank.email
Frank.age

I tried the following

function Driver(name, email, age) {
    this.name= name;
    this.email= email;
    this.age = age;
  }

and then

for(var i=0; i<data.length; i++){
  data[i][0]= new Driver (data[i][0],data[i][1],data[i][2])
}

But it is not working Can You help me with this?

3
  • Can the data array be made to an object? Example: var data= {name: 'John Doe', email: '[email protected]', age: 35}; Commented Jan 20, 2020 at 20:18
  • If you get same names, you lose the old value, [["Frank", "[email protected]", 21],["Mark", "[email protected]", 23], ["Frank", "[email protected]", 24]]. So better to convert you array to array of object and iterate them. Commented Jan 20, 2020 at 20:25
  • In case, If you want to convert into an array of objects, this might be helpful. var data = [["Frank","franksmail",2], ["name2", "mail2", 3], ["name3", "mail3", 4]] var drivers = data.map(props => new Driver(...props)) Commented Jan 20, 2020 at 20:41

1 Answer 1

5

If I understand your requirement

I would love the idea to turn the array into objects where I can access every object by the name

correctly, then the solution is as simple as this:

ES6 Version

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 Version:

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)

Warning:
If you have to Divers with the same name in your data array then you'll loose one of them.

Sign up to request clarification or add additional context in comments.

6 Comments

What if you get the same name in the array, you lose the old value. Better to convert an array of objects.
Thank you for pointing that out. I am aware of this fact, however the OP might not. (he's asking for the objects to be accessible by their name). I've added a notice
Yeah, but just to inform 😜
@Wendelin Apps Script does not support ES6, hence TheMaster's downvote.
@TheMaster oh, ok. I'm sorry. I indeed missed out on that detail. But next time please provide more specific comment. I'll update my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.