我并不是说这是一个完全聪明的想法(理想情况下,我认为应该在后端进行大量数据的计算),但我很幽默。
我试图从JSON源中提取数据并进行一些分析。鉴于以下数据来源:
[
{
"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"}
]
}
]
}
]
我想确定:
然而,我没有得到我想要的结果。我认为我的循环没有按预期工作(见评论):
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());
答案 0 :(得分:1)
卫星数量
您的JSON对象是卫星对象的数组/集合,因此length
属性是一种很好的方法。假设JSON
是加载了您拥有的JSON数据的变量,那么JSON.length
就可以了(我不会将JSON用作变量名,因为它与Javascript的内置冲突-in JSON对象。
卫星馈送数量
迭代每个卫星的单个JSON.data.length
值
最敏锐的饲料
在这里看JSFiddle - &gt; https://jsfiddle.net/pLgnv9pL/
答案 1 :(得分:1)
嗯,通过使用一些Array魔法来做我自己的版本:
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
}]);
答案 2 :(得分:0)
这是一种可以获得这些价值的方法 - 我不会使用&#39; JSON&#39;对于数据名称,如果您将其用作数据var名称,则无法使用JSON.stringify。
var data = [
{
"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"}
]
}
]
}
]
var getNumberofSatellites = data.length; //2
console.log('num sats: '+getNumberofSatellites)
console.log('group list: '+getGroupList())
console.log('most observant feed: '+getMostObservantFeed())
function getGroupList(){
var groupNamesArray = [];
for (i = 0; i < data.length; i++) {
var name = data[i].group;
groupNamesArray.push(name)
}
return groupNamesArray
};
function getMostObservantFeed(){
var topFeed = '';
var feedQuantity = 0;
for (var i = 0; i < data.length; i++) {
var sat = data[i].data;
for (var j = 0; j < sat.length; j++) {
var len = sat[j].data.length;
if (len > feedQuantity) {
feedQuantity = len
topFeed = sat[j].label;
}
}
}
return topFeed
};
&#13;