gpt4 book ai didi

javascript - 在 JavaScript 中分析来自 JSON 的数据

转载 作者:行者123 更新时间:2023-11-29 20:56:46 25 4
gpt4 key购买 nike

我并不是说这是一个完全聪明的想法(我认为对大量数据的理想计算应该在后端完成)但请幽默一下。

我正在尝试从 JSON 源中提取数据 View 片段并进行一些分析。给定以下数据源:

[
{
"group": "satellite-1",
"data": [
{
"label": "feed-1a",
"data": [
{"timeRange": [800, 820], "val": "TargetC"},
{"timeRange": [800, 820], "val": "TargetD"},
{"timeRange": [820, 840], "val": "TargetA"},
{"timeRange": [820, 840], "val": "TargetC"},
{"timeRange": [820, 840], "val": "TargetD"},
{"timeRange": [820, 840], "val": "TargetB"}
]
},
{
"label": "feed-2a",
"data": [
{"timeRange": [780, 800], "val": "TargetB"}
]
}

]
},
{
"group": "satellite-4",
"data": [
{
"label": "feed-1b",
"data": [
{"timeRange": [780, 800], "val": "TargetA"},
{"timeRange": [800, 820], "val": "TargetB"},
{"timeRange": [800, 820], "val": "TargetC"}
]
},
{
"label": "feed-2b",
"data": [
{"timeRange": [780, 800], "val": "TargetB"}
]
}

]
}
]

我要确定:

  1. 卫星数量
  2. 卫星信号数量
  3. 最观察力的提要(即哪个提要有最多的目标)

但是,我没有得到我想要的结果。我认为我的循环没有按预期工作(见评论):

var dataSet_initial = [
{
"group": "satellite-1",
"data": [
{
"label": "feed-1a",
"data": [
{"timeRange": [800, 820], "val": "TargetC"},
{"timeRange": [800, 820], "val": "TargetD"},
{"timeRange": [820, 840], "val": "TargetA"},
{"timeRange": [820, 840], "val": "TargetC"},
{"timeRange": [820, 840], "val": "TargetD"},
{"timeRange": [820, 840], "val": "TargetB"}
]
},
{
"label": "feed-2a",
"data": [
{"timeRange": [780, 800], "val": "TargetB"}
]
}

]
},
{
"group": "satellite-4",
"data": [
{
"label": "feed-1b",
"data": [
{"timeRange": [780, 800], "val": "TargetA"},
{"timeRange": [800, 820], "val": "TargetB"},
{"timeRange": [800, 820], "val": "TargetC"}
]
},
{
"label": "feed-2b",
"data": [
{"timeRange": [780, 800], "val": "TargetB"}
]
}

]
}
];

/*
* Prep Data
*/
var dataSet_stringify = JSON.stringify(dataSet_initial); // strigify JSON to parse it
var dataSet_parsed = JSON.parse(dataSet_stringify); // parse JSON

/*
* # Satellites
*/
var getNumberofSatellites = dataSet_parsed.length; //2
console.log("Number of Satellites: " + getNumberofSatellites);

/*
* # Feeds
*/
var getGroupList = function(){
var i, j;

for (i = 0; i < dataSet_parsed.length; i++) {
for (j = 0; i < dataSet_parsed[i].data.length; j++){
return dataSet_parsed[i].data[j].label;
}
}
}; //returns only the first feed, not looping through
console.log("Feeds: " + getGroupList());

/*
* # of Feed Data Feeds
*/
var getMostObservantFeed = function(){
var i, j;

for (i = 0; i < dataSet_parsed.length; i++) {
for (j = 0; i < dataSet_parsed[i].data[j].data.length; j++){
return dataSet_parsed[i].data[j].data.length;
}
}
}; //again not looping through

console.log("Individual Feed Data Feeds: " + getMostObservantFeed());

最佳答案

好吧,我为此做了自己的版本,通过使用一些数组魔法:

  const satellitesData = getSatellitesData();
const feeds = [].concat.apply([], satellitesData.map(s => s.data));

// Extracting what you want...
const satellitesCount = satellitesData.length;
const feedsCount = feeds.length;
const mostObservantFeed = feeds.reduce((a, b) => (a.data.length > b.data.length) ? a : b);

console.table([{
'Satellites count': satellitesCount,
'Feeds count': feedsCount,
'Most observant feed': mostObservantFeed.label
}]);


// Your data, which will be retrieved from somewhere...
function getSatellitesData() {
return [
{
group: 'satellite-1',
data: [
{
label: 'feed-1a',
data: [
{
timeRange: [800, 820],
val: 'TargetC'
},
{
timeRange: [800, 820],
val: 'TargetD'
},
{
timeRange: [820, 840],
val: 'TargetA'
},
{
timeRange: [820, 840],
val: 'TargetC'
},
{
timeRange: [820, 840],
val: 'TargetD'
},
{
timeRange: [820, 840],
val: 'TargetB'
}
]
},
{
label: 'feed-2a',
data: [
{
timeRange: [780, 800],
val: 'TargetB'
}]
}

]
},
{
group: 'satellite-4',
data: [
{
label: 'feed-1b',
data: [
{
timeRange: [780, 800],
val: 'TargetA'
},
{
timeRange: [800, 820],
val: 'TargetB'
},
{
timeRange: [800, 820],
val: 'TargetC'
}
]
},
{
label: 'feed-2b',
data: [
{
timeRange: [780, 800],
val: 'TargetB'
}]
}

]
}
];
}

评论版

// For the sake of clarity, I just extracted the example data to 
// a separate function...
const satellitesData = getSatellitesData();

/*
As the items of the original satellitesData array were objects, I needed to
simplify them, by making a `map` first. So, instead of an array of objects
whose items had an array property each one ("data"), after doing the map,
I'll have simply an array of arrays.
To make it even simpler, I use `[].concat.apply([], someArrayHere)`
to flatten our bidimensional array.
In the end, by using this trick, I create a flat array of feeds.
*/
const feeds = [].concat.apply([], satellitesData.map(s => s.data));


const satellitesCount = satellitesData.length;

// As I have an array of feeds above, it's just a question of getting
// the array length in order to find the feeds count
const feedsCount = feeds.length;

// Now I'm using reduce to iterate over the feeds. In each iteration,
// I'm comparing the targets count, to find the feed with more targets.
// At the end, the reduce will return the most observant feed.
const mostObservantFeed = feeds.reduce((a, b) => (a.data.length > b.data.length) ? a : b);

// To finish, console.table is just a fancy way of showing the data in the console. =)
console.table([{
'Satellites count': satellitesCount,
'Feeds count': feedsCount,
'Most observant feed': mostObservantFeed.label
}]);

关于javascript - 在 JavaScript 中分析来自 JSON 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48936860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com