javascript - How to display an array of pair arrays as key values pairs?
问题描述
I am writing a client in Ract that receives streaming data representing objects from a back end. The client parses this data and dynamically builds the object as a javascript data structure (typically an object or an array).
The backend has a Timeseries
datatype that consists of timestamp keys and arbitrary values. A Timeseries
can have duplicate timestamp keys like so:
{
'2019-08-22T18:34:15.777927965Z' : 1,
'2019-08-22T18:34:15.777927965Z' : 2,
'2019-08-22T18:34:15.777927965Z' : 3,
}
This means that I cannot use a javascript object to build this datastructure in the client given that only the last key/value pair added will be stored since the keys are the same.
My solution was to collect this data in an array:
[
['2019-08-22T18:34:15.777927965Z', 1],
['2019-08-22T18:34:15.777927965Z', 2],
['2019-08-22T18:34:15.777927965Z', 3],
]
The problem is that I have been asked to display this data in key/value format the way a javascript object would be displayed.
I am using the react-json-tree
npm package to dislpay all the objects I am constructing which just takes an object or array and formats it nicely with collapsable dropdown arrows in the UI (this is not essential).
What options do I have to turn my array of arrays in the code into a nicely formatted object like data structure in the UI?
TLDR: How do I display an array like this:
[
['2019-08-22T18:34:15.777927965Z', 1],
['2019-08-22T18:34:15.777927965Z', 2],
['2019-08-22T18:34:15.777927965Z', 3],
]
As colon separated key/value pairs in the UI, like objects?
Ideally I could pass this data structure into a library like react-json-tree
.
解决方案
You can display your data in the following way, if you want them to be given as a list :
const data = [
['2019-08-22T18:34:15.777927965Z', 1],
['2019-08-22T18:34:15.777927965Z', 2],
['2019-08-22T18:34:15.777927965Z', 3],
]
const Renderer = () =>
<ul>
{data.map(([key, val], id) => <li key={id}>{key}; {val}</li>)}
</ul>;
ReactDOM.render(<Renderer/>, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<div id="root"/>
To display it using react-json-tree
, how about wrapping your array within an object ?
const data = [
['2019-08-22T18:34:15.777927965Z', 1],
['2019-08-22T18:34:15.777927965Z', 2],
['2019-08-22T18:34:15.777927965Z', 3],
]
<JSONTree data={{log: data}} />
OR
const data = {
log : [
['2019-08-22T18:34:15.777927965Z', 1],
['2019-08-22T18:34:15.777927965Z', 2],
['2019-08-22T18:34:15.777927965Z', 3],
]
}
<JSONTree data={log} />
推荐阅读
- c# - HTMLAgilityPack InnerHTML,解析错误错误
- flutter - 如何解决 OS 错误:Permission denied, errno = 13 in flutter
- java - 在同一个项目中跨多个模块重用测试类
- blazor - Blazor:如何将 ViewModel 从子组件传递到父组件,反之亦然
- pseudocode - 如何找到乘以 i 的语句的时间复杂度
- python - 如何动态且独立地移动 pandas DataFrame 中的列?
- css - 如何突出显示导航的部分?
- angular - 如何在另一个组件中访问一个组件的变量 [Angular]
- c# - 在插入之前检查 DataGridview 中的值是否与数据库表/模式兼容
- ios - 如何以编程方式快速调整视图大小?