javascript - 如何计算.json中的对象数量
问题描述
您如何计算此数组 json 文件中不同对象的数量。单个对象是:
{
"short_name": "Demo",
"roi_number": 0,
"finding": "null",
"dt": 0.1,
"norm_times_series": [
[80.0, 68.16335617854995, 91.83664382145005],
[80.0, 67.66379980989896, 92.33620019010104],
[83.0, 72.78200156354234, 93.21799843645766],
[83.0, 71.22165069319166, 94.77834930680834],
[81.0, 67.74249757065954, 94.25750242934046],
[80.0, 67.12186571458821, 92.87813428541179],
[81.0, 68.71545011387893, 93.28454988612107]]
}
在下面的示例中,有 4 个。
[
{
"short_name": "Demo",
"roi_number": 0,
"finding": "null",
"dt": 0.1,
"norm_times_series": [
[80.0, 68.16335617854995, 91.83664382145005],
[80.0, 67.66379980989896, 92.33620019010104],
[83.0, 72.78200156354234, 93.21799843645766],
[83.0, 71.22165069319166, 94.77834930680834],
[81.0, 67.74249757065954, 94.25750242934046],
[80.0, 67.12186571458821, 92.87813428541179],
[81.0, 68.71545011387893, 93.28454988612107]]
},
{
"short_name": "Demo",
"roi_number": 1,
"finding": "null",
"dt": 0.1,
"norm_times_series": [
[80.0, 68.16335617854995, 91.83664382145005],
[80.0, 67.66379980989896, 92.33620019010104],
[83.0, 72.78200156354234, 93.21799843645766],
[83.0, 71.22165069319166, 94.77834930680834],
[81.0, 67.74249757065954, 94.25750242934046],
[80.0, 67.12186571458821, 92.87813428541179],
[81.0, 68.71545011387893, 93.28454988612107]]
},
{
"short_name": "Demo",
"roi_number": 2,
"finding": "null",
"dt": 0.1,
"norm_times_series": [
[80.0, 68.16335617854995, 91.83664382145005],
[80.0, 67.66379980989896, 92.33620019010104],
[83.0, 72.78200156354234, 93.21799843645766],
[83.0, 71.22165069319166, 94.77834930680834],
[81.0, 67.74249757065954, 94.25750242934046],
[80.0, 67.12186571458821, 92.87813428541179],
[81.0, 68.71545011387893, 93.28454988612107]]
},
{
"short_name": "Demo",
"roi_number": 3,
"finding": "null",
"dt": 0.1,
"norm_times_series": [
[80.0, 68.16335617854995, 91.83664382145005],
[80.0, 67.66379980989896, 92.33620019010104],
[83.0, 72.78200156354234, 93.21799843645766],
[83.0, 71.22165069319166, 94.77834930680834],
[81.0, 67.74249757065954, 94.25750242934046],
[80.0, 67.12186571458821, 92.87813428541179],
[81.0, 68.71545011387893, 93.28454988612107]]
}
]
我正在做反应,我将这个 json 文件作为道具传递,所以我需要操纵道具。我想知道 json 文件中有多少不同的条目,以便我可以据此显示单选按钮的数量。
import React from 'react';
const Test = (props) => {
return (
console.log(JSON.parse(props).length); // My attempt = prop is ouput.json
)
};
export default Test;
我的 json 文件在 app.js 中设置:
this.state = { apiResponse: [] };
.then(res => res.json())
.then(res => this.setState({ apiResponse: res }));
我这样称呼它
<Test test = {this.state.apiResponse}/>
解决方案
根据您的代码,如果props.data包含您提到的示例 JSON,那么props.data.length应该已经为您提供了数组中元素的数量。
如果您将道具作为字符串传递,那么只需执行
JSON.parse(props);
在访问对象的长度之前。
推荐阅读
- r - 重命名包含特定字符串的所有变量并添加序列号
- scala - Scala 类型不匹配,预期:List[NotInferedA],实际:List[Int]
- react-native - 如何在 TextInput 上长按触发一个事件,同时按下触发编辑 TextInput 值?
- sql - 如何进行查询以返回与表中每一行相关的行数据
- powershell - 已完成 Powershell 任务的时间戳
- bash - 在现有的 tmux 会话中附加并运行脚本
- laravel - 一般错误:1215 无法添加外键约束 [Laravel 5.8]
- angularjs - 更改后模型不会在指令内部刷新
- java - MQ - 获取多实例 MQ 管理器连接列表
- python - 是否可以为按 2 个元素分组的条形图添加标准误差?