Skip to content Skip to sidebar Skip to footer

Changing An Inner Object Property To An Array In Typescript

There was such a problem, I have been suffering for a couple of days, I am still quite a novice. Using the GET request to the Wikipedia API, I get this object It turns out that for

Solution 1:

If you're certain that pages will always have one property and you need only it's value, you could do something like this using Object.values:

const data = {
  batchComplete: "",
  query: {
    pages: {
      "9745": {
        pageid: 9745,
        length: 48,
        lastrevid: 100,
        contentmodel: "wikitext",
        touched: "2019-02-01"
      }
    }
  }
}

const articleInformation = {
  ...data,
  query: {
    pages: [Object.values(data.query.pages)[0]]
  }
}

console.log(articleInformation)

But, you need to have a separate interface for this.articleInformation and data since they have different structures.

Something like this:

exportinterfaceArticleInformationNew {
  batchComplete: string;
  query: {
    pages: any[]
  };
}

Post a Comment for "Changing An Inner Object Property To An Array In Typescript"