How to do a two different array results manipulation in angular6












1















Here is a product Service response.



productServiceResponse = [
{productOrderId:108898,productStatus:"C",productId:'4'},
{productOrderId:108899,productStatus:"P",productId:'2'},
];


by using const value currentProductStatus = "C";



In product Service response ,i need to find the current product and taken the product by using above constant value and taken the product id.



So here i did that .Here i have did the index of the current product instead of that need it in dynamicllay find out from the collection .



this.productServiceResponse[0].productStatus


// check the current product



  const ProductCode =
this.productServiceResponse[0].productStatus ===
this.currentProductStatus
? this.productServiceResponse[0].productId.toString()
: null;


once get the current productId.



i have check it in another collection.



      productTypes =[
{id: "1", name: "Laptop"},
{id: "2", name: "Mobile"},
{id: "3", name: "headphone"},
{id: "4", name: "Cameras"}
];


from the product types collection , i have match the product id from the above result.



   this.currentProduct = this.productTypes.find(product=>product.id ===ProductCode).name;


final o/p becomes cameras.



I have got the o/p :but i need this query in single line filteration instead of two variables.



can any one refactor this.



Demo:filter










share|improve this question

























  • stackblitz.com/edit/angular-ss14hb?file=src/app/…

    – dee zg
    Nov 24 '18 at 15:04











  • The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:17













  • i gave you flexible solution where everything is mapped correctly. now you just pick up your index you need ;)

    – dee zg
    Nov 24 '18 at 15:20
















1















Here is a product Service response.



productServiceResponse = [
{productOrderId:108898,productStatus:"C",productId:'4'},
{productOrderId:108899,productStatus:"P",productId:'2'},
];


by using const value currentProductStatus = "C";



In product Service response ,i need to find the current product and taken the product by using above constant value and taken the product id.



So here i did that .Here i have did the index of the current product instead of that need it in dynamicllay find out from the collection .



this.productServiceResponse[0].productStatus


// check the current product



  const ProductCode =
this.productServiceResponse[0].productStatus ===
this.currentProductStatus
? this.productServiceResponse[0].productId.toString()
: null;


once get the current productId.



i have check it in another collection.



      productTypes =[
{id: "1", name: "Laptop"},
{id: "2", name: "Mobile"},
{id: "3", name: "headphone"},
{id: "4", name: "Cameras"}
];


from the product types collection , i have match the product id from the above result.



   this.currentProduct = this.productTypes.find(product=>product.id ===ProductCode).name;


final o/p becomes cameras.



I have got the o/p :but i need this query in single line filteration instead of two variables.



can any one refactor this.



Demo:filter










share|improve this question

























  • stackblitz.com/edit/angular-ss14hb?file=src/app/…

    – dee zg
    Nov 24 '18 at 15:04











  • The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:17













  • i gave you flexible solution where everything is mapped correctly. now you just pick up your index you need ;)

    – dee zg
    Nov 24 '18 at 15:20














1












1








1








Here is a product Service response.



productServiceResponse = [
{productOrderId:108898,productStatus:"C",productId:'4'},
{productOrderId:108899,productStatus:"P",productId:'2'},
];


by using const value currentProductStatus = "C";



In product Service response ,i need to find the current product and taken the product by using above constant value and taken the product id.



So here i did that .Here i have did the index of the current product instead of that need it in dynamicllay find out from the collection .



this.productServiceResponse[0].productStatus


// check the current product



  const ProductCode =
this.productServiceResponse[0].productStatus ===
this.currentProductStatus
? this.productServiceResponse[0].productId.toString()
: null;


once get the current productId.



i have check it in another collection.



      productTypes =[
{id: "1", name: "Laptop"},
{id: "2", name: "Mobile"},
{id: "3", name: "headphone"},
{id: "4", name: "Cameras"}
];


from the product types collection , i have match the product id from the above result.



   this.currentProduct = this.productTypes.find(product=>product.id ===ProductCode).name;


final o/p becomes cameras.



I have got the o/p :but i need this query in single line filteration instead of two variables.



can any one refactor this.



Demo:filter










share|improve this question
















Here is a product Service response.



productServiceResponse = [
{productOrderId:108898,productStatus:"C",productId:'4'},
{productOrderId:108899,productStatus:"P",productId:'2'},
];


by using const value currentProductStatus = "C";



In product Service response ,i need to find the current product and taken the product by using above constant value and taken the product id.



So here i did that .Here i have did the index of the current product instead of that need it in dynamicllay find out from the collection .



this.productServiceResponse[0].productStatus


// check the current product



  const ProductCode =
this.productServiceResponse[0].productStatus ===
this.currentProductStatus
? this.productServiceResponse[0].productId.toString()
: null;


once get the current productId.



i have check it in another collection.



      productTypes =[
{id: "1", name: "Laptop"},
{id: "2", name: "Mobile"},
{id: "3", name: "headphone"},
{id: "4", name: "Cameras"}
];


from the product types collection , i have match the product id from the above result.



   this.currentProduct = this.productTypes.find(product=>product.id ===ProductCode).name;


final o/p becomes cameras.



I have got the o/p :but i need this query in single line filteration instead of two variables.



can any one refactor this.



Demo:filter







angular typescript rxjs angular6 rxjs6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 14:53







Mohamed Sahir

















asked Nov 24 '18 at 14:45









Mohamed SahirMohamed Sahir

60941430




60941430













  • stackblitz.com/edit/angular-ss14hb?file=src/app/…

    – dee zg
    Nov 24 '18 at 15:04











  • The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:17













  • i gave you flexible solution where everything is mapped correctly. now you just pick up your index you need ;)

    – dee zg
    Nov 24 '18 at 15:20



















  • stackblitz.com/edit/angular-ss14hb?file=src/app/…

    – dee zg
    Nov 24 '18 at 15:04











  • The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:17













  • i gave you flexible solution where everything is mapped correctly. now you just pick up your index you need ;)

    – dee zg
    Nov 24 '18 at 15:20

















stackblitz.com/edit/angular-ss14hb?file=src/app/…

– dee zg
Nov 24 '18 at 15:04





stackblitz.com/edit/angular-ss14hb?file=src/app/…

– dee zg
Nov 24 '18 at 15:04













The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

– Mohamed Sahir
Nov 24 '18 at 15:17







The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

– Mohamed Sahir
Nov 24 '18 at 15:17















i gave you flexible solution where everything is mapped correctly. now you just pick up your index you need ;)

– dee zg
Nov 24 '18 at 15:20





i gave you flexible solution where everything is mapped correctly. now you just pick up your index you need ;)

– dee zg
Nov 24 '18 at 15:20












1 Answer
1






active

oldest

votes


















2














Try this way



this.productTypes.find(product=>
product.id === productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).productID
).name


Be advised I typed this in my cell phone 🤓check this






share|improve this answer


























  • i have changed that to this.productTypes.find(product => { product.id === this.productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).map(x=>x.productID) }).map(x=>x.name); getting below error _this.productServiceResponse.find(...).map is not a function

    – Mohamed Sahir
    Nov 24 '18 at 15:03













  • sure, since map is being ran on an object and not array since find returns (first) object satisfying criteria given to it.

    – dee zg
    Nov 24 '18 at 15:09











  • Just take the .map out of the code and leave it like .productId and .name I edited my answer like so

    – Gabriel Lopez
    Nov 24 '18 at 15:16













  • @deezig o/p is wrong in your code there is no check for currentProductStatus The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:18













  • @Gabriel Lopez thanks man i have nested manipulation from your solution.keep support in stackoverflow

    – Mohamed Sahir
    Nov 24 '18 at 15:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459307%2fhow-to-do-a-two-different-array-results-manipulation-in-angular6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Try this way



this.productTypes.find(product=>
product.id === productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).productID
).name


Be advised I typed this in my cell phone 🤓check this






share|improve this answer


























  • i have changed that to this.productTypes.find(product => { product.id === this.productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).map(x=>x.productID) }).map(x=>x.name); getting below error _this.productServiceResponse.find(...).map is not a function

    – Mohamed Sahir
    Nov 24 '18 at 15:03













  • sure, since map is being ran on an object and not array since find returns (first) object satisfying criteria given to it.

    – dee zg
    Nov 24 '18 at 15:09











  • Just take the .map out of the code and leave it like .productId and .name I edited my answer like so

    – Gabriel Lopez
    Nov 24 '18 at 15:16













  • @deezig o/p is wrong in your code there is no check for currentProductStatus The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:18













  • @Gabriel Lopez thanks man i have nested manipulation from your solution.keep support in stackoverflow

    – Mohamed Sahir
    Nov 24 '18 at 15:26
















2














Try this way



this.productTypes.find(product=>
product.id === productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).productID
).name


Be advised I typed this in my cell phone 🤓check this






share|improve this answer


























  • i have changed that to this.productTypes.find(product => { product.id === this.productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).map(x=>x.productID) }).map(x=>x.name); getting below error _this.productServiceResponse.find(...).map is not a function

    – Mohamed Sahir
    Nov 24 '18 at 15:03













  • sure, since map is being ran on an object and not array since find returns (first) object satisfying criteria given to it.

    – dee zg
    Nov 24 '18 at 15:09











  • Just take the .map out of the code and leave it like .productId and .name I edited my answer like so

    – Gabriel Lopez
    Nov 24 '18 at 15:16













  • @deezig o/p is wrong in your code there is no check for currentProductStatus The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:18













  • @Gabriel Lopez thanks man i have nested manipulation from your solution.keep support in stackoverflow

    – Mohamed Sahir
    Nov 24 '18 at 15:26














2












2








2







Try this way



this.productTypes.find(product=>
product.id === productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).productID
).name


Be advised I typed this in my cell phone 🤓check this






share|improve this answer















Try this way



this.productTypes.find(product=>
product.id === productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).productID
).name


Be advised I typed this in my cell phone 🤓check this







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 15:20

























answered Nov 24 '18 at 14:59









Gabriel LopezGabriel Lopez

28217




28217













  • i have changed that to this.productTypes.find(product => { product.id === this.productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).map(x=>x.productID) }).map(x=>x.name); getting below error _this.productServiceResponse.find(...).map is not a function

    – Mohamed Sahir
    Nov 24 '18 at 15:03













  • sure, since map is being ran on an object and not array since find returns (first) object satisfying criteria given to it.

    – dee zg
    Nov 24 '18 at 15:09











  • Just take the .map out of the code and leave it like .productId and .name I edited my answer like so

    – Gabriel Lopez
    Nov 24 '18 at 15:16













  • @deezig o/p is wrong in your code there is no check for currentProductStatus The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:18













  • @Gabriel Lopez thanks man i have nested manipulation from your solution.keep support in stackoverflow

    – Mohamed Sahir
    Nov 24 '18 at 15:26



















  • i have changed that to this.productTypes.find(product => { product.id === this.productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).map(x=>x.productID) }).map(x=>x.name); getting below error _this.productServiceResponse.find(...).map is not a function

    – Mohamed Sahir
    Nov 24 '18 at 15:03













  • sure, since map is being ran on an object and not array since find returns (first) object satisfying criteria given to it.

    – dee zg
    Nov 24 '18 at 15:09











  • Just take the .map out of the code and leave it like .productId and .name I edited my answer like so

    – Gabriel Lopez
    Nov 24 '18 at 15:16













  • @deezig o/p is wrong in your code there is no check for currentProductStatus The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

    – Mohamed Sahir
    Nov 24 '18 at 15:18













  • @Gabriel Lopez thanks man i have nested manipulation from your solution.keep support in stackoverflow

    – Mohamed Sahir
    Nov 24 '18 at 15:26

















i have changed that to this.productTypes.find(product => { product.id === this.productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).map(x=>x.productID) }).map(x=>x.name); getting below error _this.productServiceResponse.find(...).map is not a function

– Mohamed Sahir
Nov 24 '18 at 15:03







i have changed that to this.productTypes.find(product => { product.id === this.productServiceResponse.find(psr=>psr.productStatus === this.currentProductStatus).map(x=>x.productID) }).map(x=>x.name); getting below error _this.productServiceResponse.find(...).map is not a function

– Mohamed Sahir
Nov 24 '18 at 15:03















sure, since map is being ran on an object and not array since find returns (first) object satisfying criteria given to it.

– dee zg
Nov 24 '18 at 15:09





sure, since map is being ran on an object and not array since find returns (first) object satisfying criteria given to it.

– dee zg
Nov 24 '18 at 15:09













Just take the .map out of the code and leave it like .productId and .name I edited my answer like so

– Gabriel Lopez
Nov 24 '18 at 15:16







Just take the .map out of the code and leave it like .productId and .name I edited my answer like so

– Gabriel Lopez
Nov 24 '18 at 15:16















@deezig o/p is wrong in your code there is no check for currentProductStatus The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

– Mohamed Sahir
Nov 24 '18 at 15:18







@deezig o/p is wrong in your code there is no check for currentProductStatus The result is wrong o/p should be camera and not both , because productStatus is C for product Id {productOrderId:108898,productStatus:"C",productId:'4'}

– Mohamed Sahir
Nov 24 '18 at 15:18















@Gabriel Lopez thanks man i have nested manipulation from your solution.keep support in stackoverflow

– Mohamed Sahir
Nov 24 '18 at 15:26





@Gabriel Lopez thanks man i have nested manipulation from your solution.keep support in stackoverflow

– Mohamed Sahir
Nov 24 '18 at 15:26




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459307%2fhow-to-do-a-two-different-array-results-manipulation-in-angular6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'