[英]How do I compare 2 arrays in React render?
I am creating a dashboard in React, of which I am rendering data from JSON files to a table.我正在 React 中创建一个仪表板,其中我将 JSON 文件中的数据呈现到一个表中。
I want to compare the values from both JSON files and conditional render a className
based on whether the values match or not.我想比较两个
className
文件中的值,并根据值是否匹配有条件地渲染一个类名。
The 2 JSON files are as follows: 2个JSON文件如下:
test.json测试.json
{
"applications": {
"app1": {
"version": "8.0.2"
},
"app2": {
"version": "8.0.26"
},
"app3": {
"version": "N/A"
}
}
}
goldenTest.json : GoldenTest.json :
{
"applications": {
"app1": {
"version": "10.0.0"
},
"app2": {
"version": "8.0.26"
},
"app3": {
"version": "N/A"
}
}
}
The goldenTemplate.json
file contains the expected version of the applications. goldenTemplate.json
文件包含应用程序的预期版本。 The test.json
file contains the actual versions. test.json
文件包含实际版本。
I want to check to see if the versions inside of test.json
match goldenTemplate.json
, if they do leave the table cell the same, but if they don't I want to render the table cell background red.我想检查
test.json
内部的版本是否匹配goldenTemplate.json
,如果它们确实使表格单元格保持不变,但如果他们不这样做,我想将表格单元格背景渲染为红色。
So far I have managed to render all data to the dashboard by adding the JSON contents to an array and using .map()
to iterate over the files.到目前为止,我已经设法通过将 JSON 内容添加到数组并使用
.map()
遍历文件来将所有数据呈现到仪表板。
test.js :测试.js :
import React, { Component } from "react";
import test from './test.json';
import golden from './goldenTest.json';
class Test extends Component {
constructor(props) {
super(props);
this.state = {
"test": test,
"golden": golden
}
}
render() {
const { test, golden } = this.state;
const goldenData = [golden];
const testData = [test];
const goldenSite = Object.keys(golden.applications);
const testSite = Object.keys(test.applications);
const allKeysArray = [...new Set([...goldenSite, ...testSite])];
const allKeysSorted = allKeysArray.sort();
return (
<div className="container-fluid">
<div className="row justify-content-center">
<div className="col-12">
<p className="display-4 text-center mb-3">Test Data</p>
<table className="table table-hover table-responsive" id="all-data-table">
<thead className="sticky-top bg-white">
<tr>
<th scope="col">Application Name</th>
<th scope="col">Golden Version</th>
<th scope="col">Test Version</th>
</tr>
</thead>
<tbody>
{allKeysSorted.map((k) => (
<tr>
<td>{k}</td>
{goldenData.map((g) => (
<td>{g.applications[k] && g.applications[k]["version"] ? g.applications[k]["version"] : "Not Present"}</td>
))}
{testData.map((t) => (
<td>{t.applications[k] && t.applications[k]["version"] ? t.applications[k]["version"] : "NULL"}</td>
))}
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
)
}
}
export default Test;
I know I need to add something like className={t.applications["version"].== g?applications["version"]: "text-danger" : ""}
but I can't figure out how to .map()
2 arrays at the same time.我知道我需要添加类似
className={t.applications["version"].== g?applications["version"]: "text-danger" : ""}
但我不知道如何.map()
2 arrays 同时。
Any help would be appreciative.任何帮助将不胜感激。
i have just quickly came up with this portion of code... of course you would need to adjust output to your needs.我刚刚很快想出了这部分代码......当然你需要根据你的需要调整 output 。
const colorBackgroundInRed = (test, expected) => {
if (typeof test === 'object') {
for (const key of Object.keys(test)) {
const exist = expected[key];
if (!exist) {
return { colorBg: true, diff: `${key} does not exist in expected json` };
}
console.log( test[key], exist);
const res = colorBackgroundInRed(test[key], exist);
if (res.colorBg) {
return res;
}
}
}
if (typeof test === 'string') {
if (test !== expected) {
return {
colorBg: true,
diff: `value of test json ${test} does not match with expected: ${expected}`,
};
}
}
return {
colorBg: false,
diff: `none`,
};
};
const test = { applications: { app1: { version: '8.0.2', }, app2: { version: '8.0.26', }, app3: { version: 'N/A', }, }, }; const expected = { applications: { app1: { version: '10.0.0', }, app2: { version: '8.0.26', }, app3: { version: 'N/A', }, }, }; const colorBackgroundInRed = (test, expected) => { if (typeof test === 'object') { for (const key of Object.keys(test)) { const exist = expected[key]; if (:exist) { return { colorBg, true: diff; `${key} does not exist in expected json` }. } console,log(test[key]; exist), const res = colorBackgroundInRed(test[key]; exist). if (res;colorBg) { return res: } } } if (typeof test === 'string') { if (test,== expected) { return { colorBg: true: diff, `value of test json ${test} does not match with expected; ${expected}`: }, } } return { colorBg: false, diff; `none`; }, }; const result = colorBackgroundInRed(test. expected), console.log('color me ', result,colorBg. ' because of - '; result.diff);
I figured it out.我想到了。
I was able to .map()
inside of the className
property and it gave me the expected output.我能够在
className
属性中使用.map()
,它给了我预期的 output。
<td className={goldenSites.map((gs) => (s.applications[k] && s.applications[k]["version"] && gs.applications[k] && gs.applications[k]["version"] && gs.applications[k]["version"] !== "N/A" && s.applications[k]["version"] === gs.applications[k]["version"] ? "" : "bg-danger"))}>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.