首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript中JSON数据的分析

JavaScript中JSON数据的分析
EN

Stack Overflow用户
提问于 2018-02-22 20:46:02
回答 3查看 540关注 0票数 2

我并不是说这是一个完全明智的想法(我认为,应该在后端对大量数据进行理想的计算),但请原谅我。

我试图从JSON源中提取视图数据片段,并进行一些分析。鉴于以下数据源:

代码语言:javascript
复制
[
    {
        "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. 最能观察的饲料(即哪种饲料有最多的目标)

然而,我没有得到我想要的结果。我认为我的循环不像预期的那样工作(见注释):

代码语言:javascript
复制
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());

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-22 21:50:25

好吧,通过使用数组魔法,我有自己的版本:

代码语言:javascript
复制
  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'
            }]
          }

        ]
      }
    ];
  }

注释版本

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2018-02-22 21:17:56

卫星数目

JSON对象是一个卫星对象的数组/集合,因此length属性是一种很好的方法。假设JSON是用您拥有的JSON数据加载的变量,那么JSON.length就可以了(我不会仅仅因为它与Javascript的内置JSON对象冲突就使用JSON作为变量名。

卫星馈送数量

迭代每个卫星的单个JSON.data.length

最能观察的饲料

见JSFiddle see -> https://jsfiddle.net/pLgnv9pL/

票数 1
EN

Stack Overflow用户

发布于 2018-02-22 21:40:02

这里有一种获取这些值的方法--我不会使用'JSON‘作为数据的名称,因为如果您使用’JSON‘作为数据变量的名称,就不能使用JSON.stringify。

代码语言:javascript
复制
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
 };

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48936860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档