简体   繁体   English

比较具有相同字段的两个数组 => 如果值不同,则在 html 表中突出显示

[英]Compare two array with same fields => if value is different highlight in html table

I have two set of array as response from an API, For example the server response is我有两组数组作为来自 API 的响应,例如服务器响应是

let original= [ {firstVal:'1.2'},{SecondVal:'3.2'}]; let original= [ {firstVal:'1.2'},{SecondVal:'3.2'}];

let latest= [{firstVal:'1.2'},{SecondVal:'4.2'}]让最新= [{firstVal:'1.2'},{SecondVal:'4.2'}]

the table will display value from latest array and logic is different value should be highlighted in bold该表将显示最新数组的值并且逻辑不同值应以粗体突出显示

| | Col1 |列 1 | Col2 |列2 |
| | 1.2 | 1.2 | 4.2 | 4.2 |

Let's say you're rendering a table.假设您正在渲染一张表格。 I'm assuming that's how visualizing this data would make sense.我假设这就是可视化这些数据的意义所在。

Then you'll store your first response and store it as its and render the default table然后您将存储您的第一个响应并将其存储为它并呈现默认表

Now when you get your latest response.现在,当您收到最新回复时。 You'll start comparing row and then column wise if there is difference then the rendered value should be surrounded with a tag to make it bold.您将开始比较行,然后按列进行比较,如果存在差异,则呈现的值应该用标记包围以使其变为粗体。 Here's a rough code这是一个粗略的代码

Assuming a react app假设一个反应应用程序


function RenderRow({row, index, latest}){
  if(latest.length < index) {
     console.log("This should not be possible")
     return null
  }

  return <tr>
  {
    row.map((column, c_index) => {
      if(latest[index][c_index] === column){
         return <td>{column}</td>
      }
      return <td><b> {column} </b></td>
    })
  }
  </tr>
}


function TableBody({original, latest}) {

  return <tbody>
   {
      original.map((row, index) => 
         <RenderRow row={row} index={index} latest={latest}/>
      )
   }
  </tbody>
}

function RenderAll(){
  let original= [ {firstVal:'1.2'},{SecondVal:'3.2'}];

  let latest= [{firstVal:'1.2'},{SecondVal:'4.2'}]
  return <Table latest={latest} original={original}/>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较 html 表中的两个单元格,如果不同则突出显示行 - Compare two cells in a html table and highlight row if different 突出显示具有相同值的输入字段 - highlight input fields with the same value 两个不同的html表突出显示相同的行顺序 - Two different html tables highlight the same rows order 如何比较html表的两列值并用颜色突出显示不匹配的数据? - how to compare html table two columns values and highlight the unmatched data with color? 比较两个数组值并在javascript中获得相同的值indexOf - compare two array value and get the same value indexOf in javascript 比较 2 两个不同的 arrays 并用 js 显示具有相同值的 object - Compare 2 two different arrays and show the object with the same value with js 比较表单元格的两个值,如果相同则添加类 - Compare Two Value Of Table Cell To Add Class If The Same 使用 javascript 比较两个数组并删除相同的值 - Compare two array and remove if same value using javascript 如何创建一个 html 表来显示具有不同字段的对象数组? - How to create an html table to display an array of objects with not the same fields? 比较两行(HTML表)数据(即行[i]与行[i + 1]),并使用Jquery / Javascript突出显示更改 - Compare two rows (HTML Table) data i.e..row[i] with row[i+1] and highlight changes using Jquery/Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM