计算Json数组中项目的大小

时间:2016-11-29 16:44:28

标签: javascript arrays json lodash

我正在构建一个JSON响应,如下所示:

{
  "G5LDUHRPEEA6B-39CFBWYA": [],
  "JMSK0DKOEEA0UXMY750O3W": [],
  "ISVN8JF1EEAD3W398ZNOSA": [
    {

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": LA,
      "hello": "OUTSIDE"

    },
    {

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": Chicago,
      "hello": “Inside"
    },
    {

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": Austin,
      "hello": “Inside"
    }
  ],
  "VRG0IJF1EEAD3W398ZNOSA": [{

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": Chicago,
      "hello": “Inside"
    },
    {

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": Austin,
      "hello": “Inside"
    }],
  "OGAESJF2EEAD3W398ZNOSA": [],
  "SC9OMJF2EEAD3W398ZNOSA": []
  }

我想计算数组中项目的总数,包括内部数组,如果内部数组为空则忽略,如#34; SC9OMJF2EEAD3W398ZNOSA":[] 我尝试了_.size(集合),但我需要知道如何删除空的内部。谢谢提前

3 个答案:

答案 0 :(得分:2)

您可以使用Array.prototype.reduce并循环来总结数组的长度 - 请参阅下面的演示:



var object={"G5LDUHRPEEA6B-39CFBWYA":[],"JMSK0DKOEEA0UXMY750O3W":[],"ISVN8JF1EEAD3W398ZNOSA":[{"delloData":"1478644629","ref":"75","dataType":"String","somePart":"LA","hello":"OUTSIDE"},{"delloData":"1478644629","ref":"75","dataType":"String","somePart":"Chicago","hello":"Inside"},{"delloData":"1478644629","ref":"75","dataType":"String","somePart":"Austin","hello":"Inside"}],"VRG0IJF1EEAD3W398ZNOSA":[{"delloData":"1478644629","ref":"75","dataType":"String","somePart":"Chicago","hello":"Inside"},{"delloData":"1478644629","ref":"75","dataType":"String","somePart":"Austin","hello":"Inside"}],"OGAESJF2EEAD3W398ZNOSA":[],"SC9OMJF2EEAD3W398ZNOSA":[]}

var result = Object.keys(object).reduce(function(p,c){
  p+= object[c].length;
  return p;
},0);


console.log(result);




答案 1 :(得分:1)

您始终可以使用lodash来计算数组中的所有项目:

var obj = {
  "G5LDUHRPEEA6B-39CFBWYA": [],
  "JMSK0DKOEEA0UXMY750O3W": [],
  "ISVN8JF1EEAD3W398ZNOSA": [
    {

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "LA",
      "hello": "OUTSIDE"

    },
    {

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "Chicago",
      "hello": "Inside"
    },
    {

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "Austin",
      "hello": "Inside"
    }
  ],
  "VRG0IJF1EEAD3W398ZNOSA": [{

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "Chicago",
      "hello": "Inside"
    },
    {

      "delloData": "1478644629",
      "ref": "75",
      "dataType": "String",
      "somePart": "Austin",
      "hello": "Inside"
    }],
  "OGAESJF2EEAD3W398ZNOSA": [],
  "SC9OMJF2EEAD3W398ZNOSA": []
  }

var result = _.flatMap(_.values(obj)).length

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.js"></script>

答案 2 :(得分:1)

使用lodash:

function calcLength(obj) {
  return _(obj)
    .values() // get the sub arrays
    .map('length') // map them to length
    .sum(); // sum the length
}

function calcLength(obj) {
  return _(obj)
    .values() // get the sub arrays
    .map('length') // map them to length
    .sum(); // sum the length
}

var obj = {
  "G5LDUHRPEEA6B-39CFBWYA": [],
  "JMSK0DKOEEA0UXMY750O3W": [],
  "ISVN8JF1EEAD3W398ZNOSA": [{

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "LA",
    "hello": "OUTSIDE"

  }, {

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "Chicago",
    "hello": "Inside"
  }, {

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "Austin",
    "hello": "Inside"
  }],
  "VRG0IJF1EEAD3W398ZNOSA": [{

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "Chicago",
    "hello": "Inside"
  }, {

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "Austin",
    "hello": "Inside"
  }],
  "OGAESJF2EEAD3W398ZNOSA": [],
  "SC9OMJF2EEAD3W398ZNOSA": []
};

console.log(calcLength(obj));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>

使用ES6,您可以使用Object.values()获取所有子数组,然后传播并连接它们。如果你有一个大对象,这可能是一个昂贵的操作。使用lodash解决方案,或简单的数组#reduce:

const calcLength = (obj) => [].concat(...Object.values(obj)).length;

const calcLength = (obj) => [].concat(...Object.values(obj)).length;

var obj = {
  "G5LDUHRPEEA6B-39CFBWYA": [],
  "JMSK0DKOEEA0UXMY750O3W": [],
  "ISVN8JF1EEAD3W398ZNOSA": [{

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "LA",
    "hello": "OUTSIDE"

  }, {

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "Chicago",
    "hello": "Inside"
  }, {

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "Austin",
    "hello": "Inside"
  }],
  "VRG0IJF1EEAD3W398ZNOSA": [{

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "Chicago",
    "hello": "Inside"
  }, {

    "delloData": "1478644629",
    "ref": "75",
    "dataType": "String",
    "somePart": "Austin",
    "hello": "Inside"
  }],
  "OGAESJF2EEAD3W398ZNOSA": [],
  "SC9OMJF2EEAD3W398ZNOSA": []
};

console.log(calcLength(obj));