question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping an array to an array and controlling the properties ?

See original GitHub issue

Hi,

I have successfully managed to do a standard mapping with some help from here, thank you.

So my destination object is like so, the keygroups property is actuall an Array of keyGroup.

export type KeyGroupPackage = {
  keyGroups?: Maybe<Array<Maybe<KeyGroup>>>;
  pageInfo?: Maybe<PageInfo>;
};

I have the paginfo working, its just doing standard property matching - so no problems there. ( i placed the code below) But I can’t get the array mapping to an array. My main schema that I run the morph

Here is my main command, to start the conversion.

morphism(keyGroupSchema, data)
export const packageSchema = createSchema<GraphQL.KeyGroupPackage, Responses.IKeyGroupRoot>({
  keyGroups: morphism(keygroupSchema), /// THIS IS A PROBLEM.
  pageInfo: morphism(paginationSchema)
})

The pageInfo works perfectly, I am placing the code below for the schema. But the keygroups does not. The keygroups is defined as an array on both source and destination.

I tried the following for the keygroup schema, which is defined above as morphism(keygroupSchema)

const keygroupSchema: StrictSchema<GraphQL.KeyGroup, Responses.IKeyGroup> = {
    keyGroupId: "dddd"
}

The problem being is that both on the source and destination, the keygroups property is actually an array of keygroup.

An keygroup is like so

export type KeyGroup = { keyGroupId: string; name?: string; }

I was hoping to use the interatee like below, but its expecting an array - so it doesn’t work.

Pagination schema - works great 😃

const paginationSchema: StrictSchema<GraphQL.PageInfo, Responses.IKeyGroupRoot> = {
  total: iteratee => iteratee.page.totalElements,
  totalPages: iteratee => iteratee.page.totalPages,
  page: iteratee => {
    if (!isUndefined(iteratee.page.number)) {
      return iteratee.page.number + 1
    }
    return 1
  },
  pageSize: iteratee => iteratee.page.size,
  hasNextPage: iteratee => {
    if (!isUndefined(iteratee.page.number) && !isUndefined(iteratee.page.totalPages)) {
      return iteratee.page.number < iteratee.page.totalPages
    }
    return false
  },
  hasPreviousPage: iteratee => {
    if (!isUndefined(iteratee.page.number)) {
      return iteratee.page.number > 1
    }
    return false
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
emyanncommented, Apr 9, 2019
const nestedSchema = createSchema<Package, typeof source>({
  keyGroups: iteratee => morphism(keygroupSchema, iteratee._embedded.records),
  pagination: morphism(paginationSchema)
})

Can also be

const nestedSchema = createSchema<Package, typeof source>({
  keyGroups: iteratee => iteratee._embedded.records.map(morphism(keygroupSchema)),
  pagination: morphism(paginationSchema)
})
1reaction
iangregsondevcommented, Apr 9, 2019

Sure let me do it now ! And thanks for the support… Back in 10 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Array Methods: how to use map and reduce
map () applies a function to each array element and creates a new array of the returned values.
Read more >
How To Use .map() to Iterate Through Array Items in JavaScript
One of the most popular methods is the .map() method. .map() creates an array from calling a specific function on each item in...
Read more >
Array.prototype.map() - JavaScript - MDN Web Docs
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array....
Read more >
Deep Dive into JavaScript's Array Map Method - Robin Wieruch
An extensive walkthrough for JavaScript developers who want to get to know the Array's Map Method to transform data.
Read more >
Map over two arrays of objects, match properties and store ...
I am trying to map through two array of objects, and if a certain property matches, pull in specific information into an array....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found