1

I'm new in VueJs and Typescript, so I have four interfaces:

export interface ShipMethodResponse {
  companyTypeList: CompanyType[]
}

export interface CompanyType {
  companyTypeId: string
  companyTypeName: string
  companyList: Company[]
}

export interface Company {
  companyId: string
  companyTypeId: string
  companyName: string
  companyAbbreviation: string
  companyDescription: string
  companyWebsite: string
  shipMethodList: ShipMethod[]
}

export interface ShipMethod {
  shipMethodId: number
  companyId: string
  shipMethodName: string
}

Each one can have an array of the next interface if you read from top to down.

So I want to insert into a new array of a string the property companyName of Company interface, so I try:

data() {
    return {
      shipMethodList: [] as string[],
      ...
    }
  },

  methods: {

    async myMethod() {
      //companyTypeList is a CompanyType array

      var b = companyTypeList.map((e) => ({
        companyList: e.companyList,
      }))

      for (const c of b) {
        var company = c.companyList.map((company) => ({
          companyName: company.companyName,
        }))

        this.shipMethodList.push(company)
      }


    }
    }

But when I try to push the company it is throwing

Argument of type '{ companyName: string; }[]' is not assignable to parameter of type 'string'.

How can I create a list of all "companyName" property? Regards

1 Answer 1

1

I think the problem is here:

var company = c.companyList.map((company) => ({
    companyName: company.companyName,
}))

You say you want to create string[], but that map function creates { companyName: string }[]

Change it to this:

var allCompanyNames = c.companyList.map(
    company => company.companyName
)

UPDATE

What you really want is probably this:

for (const c of b) {
    for(company of c.companyList) {
        this.shipMethodList.push(company.companyName)
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Now it's throwing Argument of type 'string[]' is not assignable to parameter of type 'string'
That's because you're trying to append a whole list of strings onto another list that's supposed to hold only strings. Will update answer.
Working! is there a better way to achieve this instead for inside another for? I'm talking about performance, in this case I have no problem with that, but can be useful for the future
No, if you need to collect data from two levels down, this is the fastest way to do it. for loops are faster than using Array iterators like map and forEach, because they don't incur the cost of stack thrashing. Practically speaking you will not notice any performance difference until your list has tens of thousands of entries. "Premature optimization is the root of all evil." Always write your code to be clear and correct, without worrying about performance at all, until someone proves a specific performance problem exists. Perf problems often appear in places you would never predict.

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.