0

I have the following code..

{
     this.state.rows.map((qc) =>
         qc.BinsByDayByOrchardsQCs.map((qc2) =>
             qc2.BinsByDayByOrchardsQCsDefects.map((qc3) => 
                !defectsArray.includes(qc3.Defect) && defectsArray.sort() && defectsArray.push(qc3.Defect) &&
                  (<div className="row label">
                        <div className="column-label bold">{qc3.Defect}</div>
                    </div>)
           )
         )
      )
   }

When I use console.log("DefectsArray",defectsArray)

My array is printed and sorted in the console (my desired order)

0: "Bruise"
1: "Hail damage"
2: "Sunburn"
3: "Scuff"

However, react is actually rendering it "Sunburn, Bruise, Hail damage, Scuff" - basically unordered

How do I achieve the output that is printed in my console.log?

EDIT: Inside of the object..

`Array(20)
0:
BinsByDayByOrchardsAreas: null
BinsByDayByOrchardsCountries: null
BinsByDayByOrchardsPickers: null
BinsByDayByOrchardsQCs: Array(1)
0:
BinsByDayByOrchards: null
BinsByDayByOrchardsID: null
BinsByDayByOrchardsQCsDefects: Array(4)
0: {BinsByDayByOrchardsQCsID: "-LbiHz7tuuJH71I_4IKw", Defect: "Sunburn", Count: 1, BinsByDayByOrchardsQCs: null}
1: {BinsByDayByOrchardsQCsID: "-LbiHz7tuuJH71I_4IKw", Defect: "Bruise", Count: 2, BinsByDayByOrchardsQCs: null}
2: {BinsByDayByOrchardsQCsID: "-LbiHz7tuuJH71I_4IKw", Defect: "Hail damage", Count: 0, BinsByDayByOrchardsQCs: null}
3: {BinsByDayByOrchardsQCsID: "-LbiHz7tuuJH71I_4IKw", Defect: "Scuff", Count: 2, BinsByDayByOrchardsQCs: null}

`

4
  • Possible duplicate of Sorting mapped properties from array JavaScript Commented Apr 28, 2019 at 22:57
  • @Dom link is broken! Commented Apr 29, 2019 at 3:25
  • what is the structure of qc ? Commented Apr 29, 2019 at 3:49
  • @NihalSaxena I've updated my post Commented Apr 29, 2019 at 3:52

1 Answer 1

1

This is a simple working example for your reference as a code snippet!

function App(){
  return(
    [ "Hail damage", "Sunburn","Bruise","Scuff"].sort().map((s)=><div>{s}</div>)
  );
}

ReactDOM.render(
  <App />,
  document.body
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

However your code is so flawed, it does't make sense at all. For starters you are sorting the defects array but you are showing(rendering) values that are in the qc3 array!

Move the function to generate strings for display into another function. I have fixed the rest of the implementation to be more readable!

getDisplayValues() {
    let displayedDefects = [];
    this.state.rows.map((qc) =>
        qc.BinsByDayByOrchardsQCs.map((qc2) =>
            qc2.BinsByDayByOrchardsQCsDefects.map((qc3) =>
                if(defectsArray.includes(qc3.Defect)){
                    displayedDefects.push(qc3.Defect);
                }
            )
        )
    );

    displayedDefects.sort();

    return displayedDefects.map((defect)=>( 
      <div className = "row label" >
          <div className = "column-label bold" > {defect} </div>
      </div> );

}

render(){
    return {this.getDisplayValues()}               
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried your code, fixed some syntax but displayedDefects is empty, but yes you are right, I was sorting the defectsArray but displaying qc3.Defects....that was my attempt at sorting the array and displaying it

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.