Skip to main content
Code Review

Return to Answer

deleted 62 characters in body
Source Link

Addressing Your Main Question

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code within the map function callback can be simplified using that method instead of calling the forEach() method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(element['state-province'], element.country, element.name);
 });
})

Notice there is no need to declare the array- i.e. uniArr because the map method returns an array.

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(element['state-province'], element.country, element.name)
 ));
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Review

While there isn't much code to review here, the present code appears quite readable. Indentation is consistent and variables have acceptable names.

It would be wise to check respData to ensure it is an array before calling a method like forEach() or map() on it, and if the code doesn't already catch exceptions from the call to this.http.get() then it would be wise to also handle those cases. For example, what happens if the network fails or the API is not available?

Addressing Your Main Question

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code within the map function callback can be simplified using that method instead of calling the forEach() method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(element['state-province'], element.country, element.name);
 });
})

Notice there is no need to declare the array- i.e. uniArr because the map method returns an array.

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(element['state-province'], element.country, element.name
 ));
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Review

While there isn't much code to review here, the present code appears quite readable. Indentation is consistent and variables have acceptable names.

It would be wise to check respData to ensure it is an array before calling a method like forEach() or map() on it, and if the code doesn't already catch exceptions from the call to this.http.get() then it would be wise to also handle those cases. For example, what happens if the network fails or the API is not available?

Addressing Your Main Question

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code within the map function callback can be simplified using that method instead of calling the forEach() method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(element['state-province'], element.country, element.name);
 });
})

Notice there is no need to declare the array- i.e. uniArr because the map method returns an array.

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(element['state-province'], element.country, element.name)
 );
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Review

While there isn't much code to review here, the present code appears quite readable. Indentation is consistent and variables have acceptable names.

It would be wise to check respData to ensure it is an array before calling a method like forEach() or map() on it, and if the code doesn't already catch exceptions from the call to this.http.get() then it would be wise to also handle those cases. For example, what happens if the network fails or the API is not available?

Bounty Awarded with 50 reputation awarded by Community Bot
added 183 characters in body
Source Link

Addressing Your Main Question

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code within the map function callback can be simplified using that method instead of calling the forEach() method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(
 element['state-province'],
 element.country,
 element.name
 );
 });
})

Notice there is no need to declare the array- i.e. uniArr because the map method returns an array.

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
 ));
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Review

While there isn't much code to review here, the present code appears quite readable. Indentation is consistent and variables have acceptable names.

It would be wise to check respData to ensure it is an array before calling a method like forEach() or map() on it, and if the code doesn't already catch exceptions from the call to this.http.get() then it would be wise to also handle those cases. For example, what happens if the network fails or the API is not available?

Addressing Your Main Question

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code can be simplified using that method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(
 element['state-province'],
 element.country,
 element.name
 );
 });
})

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
 ));
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Review

While there isn't much code to review here, the present code appears quite readable. Indentation is consistent and variables have acceptable names.

It would be wise to check respData to ensure it is an array before calling a method like forEach() or map() on it, and if the code doesn't already catch exceptions from the call to this.http.get() then it would be wise to also handle those cases. For example, what happens if the network fails or the API is not available?

Addressing Your Main Question

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code within the map function callback can be simplified using that method instead of calling the forEach() method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(
 element['state-province'],
 element.country,
 element.name
 );
 });
})

Notice there is no need to declare the array- i.e. uniArr because the map method returns an array.

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
 ));
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Review

While there isn't much code to review here, the present code appears quite readable. Indentation is consistent and variables have acceptable names.

It would be wise to check respData to ensure it is an array before calling a method like forEach() or map() on it, and if the code doesn't already catch exceptions from the call to this.http.get() then it would be wise to also handle those cases. For example, what happens if the network fails or the API is not available?

added 531 characters in body
Source Link

Addressing Your Main Question

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code can be simplified using that method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(
 element['state-province'],
 element.country,
 element.name
 );
 });
})

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
 ));
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Review

While there isn't much code to review here, the present code appears quite readable. Indentation is consistent and variables have acceptable names.

It would be wise to check respData to ensure it is an array before calling a method like forEach() or map() on it, and if the code doesn't already catch exceptions from the call to this.http.get() then it would be wise to also handle those cases. For example, what happens if the network fails or the API is not available?

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code can be simplified using that method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(
 element['state-province'],
 element.country,
 element.name
 );
 });
})

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
 ));
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Addressing Your Main Question

I would like to know if this is the best/most efficient/industry standard way of converting from the http response to an array of typed values

Just as an array has its forEach() method it also has a map() method. That method essentially pushes the return value of the callback function into an array and returns the array. Because of this the code can be simplified using that method.

Thus this inner block:

.pipe(map(respData => {
 const uniArr: University[] = [];
 respData.forEach(element => {
 uniArr.push(new University(element['state-province'], element.country, element.name));
 });
 return uniArr;
})

can be simplified to:

.pipe(map(respData => {
 return respData.map(element => {
 return new University(
 element['state-province'],
 element.country,
 element.name
 );
 });
})

And that can be simplified because there is only a single statement being returned

.pipe(map(respData => {
 return respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
 ));
})

And the return before the call to the map method could also be removed though it might make for a long line that some would split

.pipe(map(respData => respData.map(element => new University(
 element['state-province'],
 element.country,
 element.name
))))

Using the map method approach there are no side-effects of the callback function and thus it is a pure function. This means it is simpler to test, and allows for fewer indentation levels.

1

Review

While there isn't much code to review here, the present code appears quite readable. Indentation is consistent and variables have acceptable names.

It would be wise to check respData to ensure it is an array before calling a method like forEach() or map() on it, and if the code doesn't already catch exceptions from the call to this.http.get() then it would be wise to also handle those cases. For example, what happens if the network fails or the API is not available?

deleted 6 characters in body
Source Link
Loading
Source Link
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /