У меня есть массив информации о сотруднике в моем состоянии реагирования. Я отображаю его в таблице и пытаюсь использовать функцию OrderBy в loda sh для сортировки их по определенным ключам в ответе на опрос, который находится в строке JSON, которую они могут иметь или не иметь. У некоторых сотрудников есть ответ на опрос, а у других нет. Некоторые сотрудники не имеют одинаковых ключей в своем ответе. Ниже приведены 2 примера разобранных опросов, а также фотографии основных массивов, которые я пытаюсь отсортировать. Я пытаюсь отсортировать сотрудника на основе ExpValue, где «ExperienceTypeID»: 3, «ExpType»: «AllExperience». Ниже приведен код, который я использую для сопоставления массива и отображения сотрудников. Он также показывает код условной сортировки, который я пытался использовать. Я не знаю, где go, и мне интересно, возможно ли это вообще сделать.
Пример 1:
[{"QuestionID":1,"Answer":3},{"QuestionID":2,"Answer":"Yes"},{"QuestionID":5,"Answer":"Yes"},
{"QuestionID":8,"Answer":"Yes"},{"QuestionID":12,"Answer":"word"},{"QuestionID":13,"Answer":"No"},
{"QuestionID":15,"Answer":"Doctorate"},{"QuestionID":16,"Answer":"Yes"},{"QuestionID":18,"Answer":"No"},
{"QuestionID":32,"Answer":3},{"QuestionID":17,"Answer":"AL,AZ"},
{"ExperienceAreas":[
{"ExperienceTypeID":3,"ExpType":"AllExperience","ExpValue":"1"},
{"ExperienceTypeID":4,"ExpType":"AllExperience","ExpValue":"2"},
{"ExperienceTypeID":5,"ExpType":"AllExperience","ExpValue":"3"},
{"ExperienceTypeID":6,"ExpType":"AllExperience","ExpValue":"4"},
{"ExperienceTypeID":7,"ExpType":"AllExperience","ExpValue":"5"},
{"ExperienceTypeID":1,"ExpType":"AllExperience","ExpValue":"6"},
{"ExperienceTypeID":9,"ExpType":"AllExperience","ExpValue":"7"},
{"ExperienceTypeID":2,"ExpType":"AllExperience","ExpValue":"8"},
{"ExperienceTypeID":8,"ExpType":"AllExperience","ExpValue":"9"},
{"ExperienceTypeID":3,"ExpType":"AL","ExpValue":"1"},{"ExperienceTypeID":4,"ExpType":"AL","ExpValue":"1"},
{"ExperienceTypeID":5,"ExpType":"AL","ExpValue":"1"},{"ExperienceTypeID":6,"ExpType":"AL","ExpValue":"1"},
{"ExperienceTypeID":7,"ExpType":"AL","ExpValue":"1"},{"ExperienceTypeID":1,"ExpType":"AL","ExpValue":"1"},
{"ExperienceTypeID":9,"ExpType":"AL","ExpValue":"1"},{"ExperienceTypeID":2,"ExpType":"AL","ExpValue":"1"},
{"ExperienceTypeID":8,"ExpType":"AL","ExpValue":"1"},{"ExperienceTypeID":3,"ExpType":"AK","ExpValue":"2"},
{"ExperienceTypeID":4,"ExpType":"AK","ExpValue":"2"},{"ExperienceTypeID":5,"ExpType":"AK","ExpValue":"2"},
{"ExperienceTypeID":6,"ExpType":"AK","ExpValue":"2"},{"ExperienceTypeID":7,"ExpType":"AK","ExpValue":"2"},
{"ExperienceTypeID":1,"ExpType":"AK","ExpValue":"2"},{"ExperienceTypeID":9,"ExpType":"AK","ExpValue":"2"},
{"ExperienceTypeID":2,"ExpType":"AK","ExpValue":"2"},{"ExperienceTypeID":8,"ExpType":"AK","ExpValue":"2"}]}]
Пример 2:
[{"QuestionID":1,"Answer":5},{"QuestionID":2,"Answer":"No"},{"QuestionID":10,"Answer":"Yes"},
{"QuestionID":13,"Answer":"No"},{"QuestionID":15,"Answer":"Associates"},{"QuestionID":16,"Answer":"No"},
{"QuestionID":18,"Answer":"No"},{"QuestionID":32,"Answer":4}]
Изображение основного массива
Изображение ключей в основном массиве, где результат опроса равен нулю
Изображение ключей в основном массиве, где результат опроса не равен нулю, и в JSON строке
'' '
<table style={{overflow: "hidden"}}>
<tbody>
<tr style={{height: "160px"}}>
<th onClick={() => this.onOrderByProperty('TenureInDays')} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "105px" }}>Add Employee</span></div></th>
<th onClick={() => this.onOrderByProperty('EmployeeName')} className="rotate1"><div style={{paddingTop: "18px", paddingLeft: "11px", marginTop: "23px"}}><span className="spanUnderline1" style={{ paddingRight: "80px", paddingLeft: "54px", paddingTop: "37px" }}>Employee Name</span></div></th>
<th onClick={() => this.onOrderByProperty(1)} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "140px" }}>Curative</span></div></th>
<th onClick={() => this.onOrderByProperty(2)} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "109px" }}>Due Diligence</span></div></th>
<th onClick={() => this.onOrderByProperty(3)} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "70px" }}>Full Title Abstracting</span></div></th>
<th onClick={() => this.onOrderByProperty(4)} className="rotate1"><div><span className="spanUnderline1" >Land and Lease Administration</span></div></th>
<th onClick={() => this.onOrderByProperty(7)} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "102px" }}>Mineral Buying</span></div></th>
<th onClick={() => this.onOrderByProperty(9)} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "116px" }}>Title Opinion</span></div></th>
<th onClick={() => this.onOrderByProperty(5)} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "115px" }}>Lease Check</span></div></th>
<th onClick={() => this.onOrderByProperty(6)} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "143px" }}>Leasing</span></div></th>
<th onClick={() => this.onOrderByProperty(8)} className="rotate1"><div><span className="spanUnderline1" style={{ paddingRight: "48px" }}>ROW</span></div></th>
</tr>
{orderBy(this.state.results, [ this.state.results.filter(x => x.SurveyResponse !== null)
? JSON.parse(this.state.results.filter(x => x.SurveyResponse !== null)[0].SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(this.state.results.filter(x => x.SurveyResponse !== null)[0].SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(this.state.results.filter(x => x.SurveyResponse !== null)[0].SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(this.state.results.filter(x => x.SurveyResponse !== null)[0].SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === this.state.orderByProperty && h.ExpType === "AllExperience")
? JSON.parse(this.state.results.filter(x => x.SurveyResponse !== null)[0].SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === this.state.orderByProperty && h.ExpType === "AllExperience")[0]
? JSON.parse(this.state.results.filter(x => x.SurveyResponse !== null)[0].SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === this.state.orderByProperty && h.ExpType === "AllExperience")[0].ExpValue
: "SurveyResponse" : "SurveyResponse" : "SurveyResponse" : "SurveyResponse" : "SurveyResponse" : "SurveyResponse"], [this.state.orderByDirection]).map(x => <tr key={x.EmployeeID} className="rows" >
<td >
<label>
<input type="checkbox" name={"Employee_" + x.EmployeeID} value={x.EmployeeID} className="filled-in" onChange={e => this.checkedResource(e)}/>
<span> </span>
</label>
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)}>{x.EmployeeName}</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 1 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 1 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 1 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 2 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 2 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 2 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 3 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 3 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 3 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 4 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 4 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 4 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 7 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 7 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 7 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 9 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 9 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 9 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 5 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 5 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 5 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 6 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 6 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 6 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
<td onClick={() => this.openSurvey(x.EmployeeID)} style={{textAlign: "center"}}>
{x.SurveyResponse ? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 8 && h.ExpType === "AllExperience")
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 8 && h.ExpType === "AllExperience")[0]
? JSON.parse(x.SurveyResponse).filter(z => z.ExperienceAreas)[0]["ExperienceAreas"].filter(h => h.ExperienceTypeID === 8 && h.ExpType === "AllExperience")[0].ExpValue
: null : null : null : null : null : null}
</td>
</tr>)}
</tbody>
</table>