0

I know that there is a lot of thread that already answer about this nested loop using map in react js problem, but I'am quite confused as how can I implemented it in my code. I tried several time but got an an error,

this is some topic that I tried, but I can't seem to implement it:

  1. react-create-nested-components-with-loops

  2. how-to-have-nested-loops-with-map-in-jsx

  3. react-map-over-nested-array-of-objects

this is my Json that I want to get:

"costs": [
          {
            "service": "CTC",
            "description": "JNE City Courier",
            "cost": [
              {
                "value": 234000,
                "etd": "1-2",
                "note": ""
              }
            ]
          },
          {
            "service": "CTCYES",
            "description": "JNE City Courier",
            "cost": [
              {
                "value": 468000,
                "etd": "1-1",
                "note": ""
              }
            ]
          }
        ]

What I want is to get the value from this JSON example, but still no luck

this is my component that I want to loop:

<MDBDropdown className="select-type">
 <MDBDropdownToggle caret className="select-btn">
  Choose Your Courier Service
 </MDBDropdownToggle>
 <MDBDropdownMenu basic onClick={this.serviceData}>
  {shipmentFees != null ? shipmentFees.map(
    shipmentFee => (
     <MDBDropdownItem key={shipmentFee.cost.service} name={shipmentFee.cost.description + "," + shipmentFee.cost.etd} value={shipmentFee.cost.value}>
      {shipmentFee.cost.description}, {shipmentFee.cost.etd} Days
     </MDBDropdownItem>
    )
   )
  :
    <MDBDropdownItem value="-">There is no service</MDBDropdownItem>
  }
 </MDBDropdownMenu>
</MDBDropdown>

from reference number 3, I tried this solution but I got unexpected token, expected ","

<MDBDropdown className="select-type">
 <MDBDropdownToggle caret className="select-btn">
  Choose Your Courier Service
 </MDBDropdownToggle>
 <MDBDropdownMenu basic onClick={this.serviceData}>
  {shipmentFees != null ? shipmentFees.map(
   shipmentFee => (
    {
     shipmentFee.cost.map(
      cost => (
       <MDBDropdownItem key={cost.service} name={cost.description + "," + cost.etd} value={cost.value}>
        {cost.description}, {cost.etd} Days
       </MDBDropdownItem>
      )
     )}
    )
   )
   :
   <MDBDropdownItem value="-">There is no service</MDBDropdownItem>
  }
 </MDBDropdownMenu>
</MDBDropdown>

can someone help me to solve this?

3
  • remove the brackets around shipmentFee.cost.map(....) Commented Nov 30, 2019 at 15:13
  • @RamKrish2079 Thankyou for your response, When I remove the brackets as you say, the unexpected token, expected "," error is gone but still I can't get the cost.value from the JSON, just for information, it appears in console.log Commented Nov 30, 2019 at 15:19
  • considering shipmentFees is the JSON you provided it should work Commented Nov 30, 2019 at 15:25

1 Answer 1

1

I just follow your 2nd block of code

1) I think your shipmentFee.cost.map( should be shipmentFee.costs.map(

2) Next line cost => ( here cost will give you service, description and cost[]

3) cost[] as it is an array you have to do another map/loop to extract value, etd and note. this value is your expected value I think

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

4 Comments

After I tried this it works, I must tweek my code a little bit but it works when I follow your step slowly with some trial and error thank you
Glad to here that my solution helps you
but I want to ask one thing, do you know how to get event.target.value from a nested loop?
event.target.value is not a part of nested loop. Let say you have a button <input type="text" onChange={this.handleChange}/> and handler method handleChange=(event)=>{...}, once you try to change that text box it will fire and you will get the event.target.value

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.