0

I'm getting an a response from a server which is javascript object which has array in itself, and I would like to do some operation in client - I would like to simplify that by converting response to some simple kind of string which I would like to display to user, here's the code:

Server response looks like this:

0: {productId: 25, productType: 2, productCodes: Array(3)}
1: {productId: 26, productType: 1, productCodes: Array(3)}
length: 2


productCodes: Array(3)
0: {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description1"}
1: {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description2"}
2: {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description3"}

How can I convert this to create new array to get something like this:

{productType:2, productCodes: "Description1 (PRODUCT01), Description2 (PRODUCT02), Description3 (PRODUCT03)" }
{productType:1...}

So basically to create a string which as Description and Code in braces ( ) with comma separated values?

I have tried something like this but this didn't lead me anywhere:

// const data = response.data.productCodes.map((currElement, index) => ({
        //   productType = currElement.transactionType,
        //   productCodes = currElement.productCodes.map((elem, index)=>({
        //   }))
 // }));

Any kind of help would be awesome!

Thanks !

2
  • You want to use .map() on each product to convert it to Description1 (PRODUCT01), and then .join(', ') on the result of that to join them all Commented Oct 15, 2020 at 16:59
  • The response from that server is presumably a JSON string, rather than a JavaScript object. You create a JavaScript object using JSON.parse() on the returned string. Commented Oct 15, 2020 at 16:59

3 Answers 3

1

You want to use .map() on each product to convert it to Description1 (PRODUCT01), and then .join(', ') on the result of that to join them all

const products = [
 {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description1"},
 {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description2"},
 {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description3"}];

 const combinedString = products.map(p => `${p.desc} (${p.code})`).join(', ');
 console.log('combinedString:', combinedString);

Sign up to request clarification or add additional context in comments.

Comments

0
<!DOCTYPE html>
<html>
<body>

<p>Click the button to join the array elements into a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
function myFunction() {
  var productCodes = [
  {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description1"},
  {productId: 0, productType: 0, code: "PRODUCT02", desc: "Description2"},
  {productId: 0, productType: 0, code: "PRODUCT03", desc: "Description3"}];
  
  var result = productCodes.map(x=>x.desc+'('+x.code+')');
  var x = document.getElementById("demo");
  x.innerHTML = result.join();
  // result: Description1(PRODUCT01),Description2(PRODUCT02),Description3(PRODUCT03)
}
</script>

</body>
</html>

Comments

0
  • Using Array.reduce, you can generate the combined product string from productCodes array.
  • Using Array.map, you can map the generated combined product string to productCodes item.

const products = [{
  productId: 25,
  productType: 2,
  productCodes: [
    {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description1"},
    {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description2"},
    {productId: 0, productType: 0, code: "PRODUCT01", desc: "Description3"}
  ]
}];

const result = products.map(({ productCodes, ...rest }) => ({
  ...rest,
  productCodes: productCodes.reduce((acc, curV, curI) => (acc += (curI > 0 ? ", " : "") + curV.desc + " (" + curV.code + ")"), '')
}));
console.log(result);

Comments

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.