0

Alright, I must be missing something here. I have an object:

export class Country {
    id: number = 0
    name: string = ''

    getNamePlusBob() {
        return this.name + 'bob'
    }

}

And I get the data from an API in an array, then pass it to the browser.

Once i have the data I want to turn the array into an array of objects of type 'Country'.

So I made this function (not sure if Hydrate is the correct term here?):

function hydrateArray(data, entity) {
    let returnArray = []
    console.log(data)
    data.forEach((row) => {
        const object = entity;
        for (const [key, value] of Object.entries(row)) {
            if(Object.hasOwn(object, key)) {
                object[key] = value
            } else {
                console.log('Key not found in entity', key)
            }
        }
        console.log(object.getNamePlusBob())
        returnArray.push({...object})
    })
    return returnArray
}

const countries = hydrateArray(countryData, new Country())

In the console.log in the function, I can run the getNamePlusBob() and it returns the name plus bob. However, on the returned array countries, I cannot:

console.log(countries[0].getNamePlusBob())

TypeError: countries[0].getNamePlusBob is not a function

Why not? It was inside the hydrateArray function, why cant I run it outside?

Richard Bridge
  • 318
  • 1
  • 3
  • 10
  • `getNamePlusBob` is not an own-property on the object created by doing `new Country()` (ie: `entity`), it is a property on its prototype, and properties/methods on the prototype aren't copied when using the spread syntax `...`. You should be calling `new Country()` within your `hydrateArray` array if you want to create new country instances. – Nick Parsons Jan 26 '23 at 11:07
  • how do I do that, as I want the function be able to perform the task with any object I give it? – Richard Bridge Jan 26 '23 at 11:15
  • If you want it to be generic, then you can keep the code as it currently is, but use `Object.create()` to define `object` within your object to keep the prototype from `entity`. – Nick Parsons Jan 26 '23 at 11:25
  • @NickParsons so instead of `object = entity` I have replaced it with `const object = Object.create(entity)` and I still cannot run the plusBob function on the returned array. – Richard Bridge Jan 27 '23 at 14:40
  • You should only be pushing object now, you don’t need to spread anymore as Object.create will make a unique object for you – Nick Parsons Jan 27 '23 at 22:42
  • @NickParsons Thank you for help, I really appreciatte it but it is stil failing. Using Object.Create, and `returnArrray.push(object)`, The function now works but there is no data. In the console it is writing `Key not found in entity`. None of the keys are able to be found in the object when using `Object.create()` – Richard Bridge Jan 30 '23 at 09:30
  • oh yes, you'll also need to ensure that you change the condition for `Object.hasOwn(object, key)` as `object` is now a new object with no own-properties, you'll want to do `Object.hasOwnn(entity, key)` – Nick Parsons Jan 30 '23 at 10:54
  • @NickParsons yes that works great now! Thank you, we got there in the end. Do you want to put it as an answer? Otherwise i'll add the final code here. – Richard Bridge Feb 06 '23 at 11:10
  • That’s great, as this was closed as a duplicate, I can’t add an answer (most of what I’ve mentioned is covered in the links above your question). – Nick Parsons Feb 06 '23 at 13:30

0 Answers0