【问题标题】:How can I count how many times something appears in my object?我如何计算某物在我的对象中出现的次数?
【发布时间】:2015-11-18 10:27:55
【问题描述】:

我需要查看代码在海关键中出现的次数,然后在 (name:) 下显示代码以及它在 (data:) 中出现的次数。我想我很接近请看下面的sn-p。因此,当我控制台日志数据时,我只想看到类似 - 名称:123213 数据:22。

// Here is my json object 
var json = [
    "G": "PIF",
    "H": "FOB",
    "I": "NINGBO",
    "J": "NGB",
    "K": "2014-10-01",
    "M": "2014-10-01",
    "Y": "LIVERPOOL",
    "zp": "LIV",
    "N": "2014-11-09",
    "P": "2014-11-09",
    "R": "2014-11-09T12:01",
    "V": true,
    "zk": " ",
    "zo": "7",
    "Customs": [
        "39210000"
    ],
    "LatLon": {}
},
//   ...... etc

// Here is my failed attempt 
$(document).ready(function () {
    var CommodityCounts = {};
    var Commoditycds  = [];
    var totalCount    = 0;

    //loop through the object
    $.each(json, function(key, val) {
        var Commoditycd = val["Customs"];

        //build array of unique country names
        if ($.inArray(Commoditycd, Commoditycds) == -1) {
            Commoditycds.push(Commoditycd);
        }

        //add or increment a count for the country name
        if (typeof CommodityCounts[Commoditycd] == 'undefined') {
            CommodityCounts[Commoditycd] = 1;
        }
        else {
            CommodityCounts[Commoditycd]++;
        }

        //increment the total count so we can calculate %
        totalCount++;
    });

    //console.log(Commoditycds);
    var data = [];

    //loop through unique countries to build data for chart
    $.each(Commoditycds, function(key, Commoditycd) {
        data.push({
            name: Commoditycd,
            data: CommodityCounts
        });
    });
    console.log(data);
});

// Need the data to be show like (name of the code and how many times it appears in my json object-  name: '123123', data: [83]

【问题讨论】:

  • name:123213 data:22 与您发布的数组有什么关系?我不明白预期的行为。编辑:我认为您希望名称与海关属性相匹配,但仍然不清楚应该是什么数据以及如果数组中有多个对象,您期望什么。您必须使用 MCVE 复制问题来编辑您的问题
  • 仅供参考,您拥有的是对象数组,而不是 JSON。
  • customs 数组中可以有多个数字吗?
  • @RoryMcCrossan 不错的一个
  • 你从哪里得到123213?为什么是data: 22

标签: javascript jquery json object


【解决方案1】:

我的版本与 Rūdolfs 的版本非常相似,只是我使用 map 而不是 reduce 来构建新数组。它还会检查 Customs 属性是否存在。

var out = arr.reduce(function (p, c) {
    if (c.Customs) {
        c.Customs.forEach(function (el) {
            p[el] = (p[el] || 0) + 1;
        });
    }
    return p;
}, {});

var out = Object.keys(out).map(function (key) {
  return { name: key, value: out[key] };
});

DEMO

【讨论】:

  • map 确实更合适。 +1
【解决方案2】:

应该这样做。

var result = json.reduce(function(a, x) {
  x.Customs.forEach(function(c) {
    a[c] = a[c] ? a[c] + 1 : 1
  });
  return a;
}, {});

result = Object.keys(result).reduce(function(a, key) {
  return a.concat([{name: key, data: result[key]}])
}, []);

console.log(result);

供参考:

【讨论】:

  • Cannot read property 'forEach' of undefined --- 你是如何定位“Customs”键的?
  • aaaa 我看到这行得通 :) 但我认为我的失败是因为某些部分不包含海关密钥。
  • @user5554551 您可以在 reduce 中添加检查或在减少之前应用过滤器,例如:jsfiddle.net/hjrtx4rf/3
猜你喜欢
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多