计算先前元素的数量

时间:2019-01-28 14:02:57

标签: javascript

我有一个这样的对象数组:

const data = [
  {
    "continent": "Europe",
    "year": 2016,
    "state": " mx l",
    "value": 93.10611646419025
  },
  {
    "continent": "Europe",
    "year": 2016,
    "state": "Q  xe",
    "value": 46.14966763264863
  },
  {
    "continent": "Europe",
    "year": 2017,
    "state": " mx l",
    "value": 29.392192664199012
  },
  {
    "continent": "Europe",
    "year": 2017,
    "state": "Q  xe",
    "value": 14.676226327897535
  },
  {
    "continent": "Europe",
    "year": 2018,
    "state": " mx l",
    "value": 7.9210338610822495
  },
  {
    "continent": "Europe",
    "year": 2018,
    "state": "Q  xe",
    "value": 42.77961684001821
  },
  {
    "continent": "Europe",
    "year": 2019,
    "state": " mx l",
    "value": 30.195477072260847
  },
  {
    "continent": "Europe",
    "year": 2019,
    "state": "Q  xe",
    "value": 0.4764215061746846
  },
  {
    "continent": "Asia",
    "year": 2016,
    "state": "Mxhvisio",
    "value": 52.184301395612096
  },
  {
    "continent": "Asia",
    "year": 2016,
    "state": "Velgbvsy",
    "value": 66.56540671530074
  },
  {
    "continent": "Asia",
    "year": 2016,
    "state": "Otxenpwe",
    "value": 81.28417729926467
  },
  {
    "continent": "Asia",
    "year": 2017,
    "state": "Mxhvisio",
    "value": 24.959281347996697
  },
  {
    "continent": "Asia",
    "year": 2017,
    "state": "Velgbvsy",
    "value": 62.620083230638166
  },
  {
    "continent": "Asia",
    "year": 2017,
    "state": "Otxenpwe",
    "value": 29.259822764307053
  },
  {
    "continent": "Asia",
    "year": 2018,
    "state": "Mxhvisio",
    "value": 97.99032287910472
  },
  {
    "continent": "Asia",
    "year": 2018,
    "state": "Velgbvsy",
    "value": 15.553958337919838
  },
  {
    "continent": "Asia",
    "year": 2018,
    "state": "Otxenpwe",
    "value": 1.0460838512473591
  },
  {
    "continent": "Asia",
    "year": 2019,
    "state": "Mxhvisio",
    "value": 36.11846533794167
  },
  {
    "continent": "Asia",
    "year": 2019,
    "state": "Velgbvsy",
    "value": 25.467981394020022
  },
  {
    "continent": "Asia",
    "year": 2019,
    "state": "Otxenpwe",
    "value": 59.55173397523441
  },
  {
    "continent": "Africa",
    "year": 2016,
    "state": "Oqkaqap",
    "value": 66.8220176856509
  },
  {
    "continent": "Africa",
    "year": 2016,
    "state": " vnzkxo",
    "value": 11.062951843116519
  },
  {
    "continent": "Africa",
    "year": 2016,
    "state": "Juucqrd",
    "value": 8.482606846746087
  },
  {
    "continent": "Africa",
    "year": 2017,
    "state": "Oqkaqap",
    "value": 78.48483030953402
  },
  {
    "continent": "Africa",
    "year": 2017,
    "state": " vnzkxo",
    "value": 93.20229532997375
  },
  {
    "continent": "Africa",
    "year": 2017,
    "state": "Juucqrd",
    "value": 96.36196870652273
  },
  {
    "continent": "Africa",
    "year": 2018,
    "state": "Oqkaqap",
    "value": 18.806971985682488
  },
  {
    "continent": "Africa",
    "year": 2018,
    "state": " vnzkxo",
    "value": 59.864704301091365
  },
  {
    "continent": "Africa",
    "year": 2018,
    "state": "Juucqrd",
    "value": 77.49958555283216
  },
  {
    "continent": "Africa",
    "year": 2019,
    "state": "Oqkaqap",
    "value": 55.113253844664015
  },
  {
    "continent": "Africa",
    "year": 2019,
    "state": " vnzkxo",
    "value": 20.65153716524726
  },
  {
    "continent": "Africa",
    "year": 2019,
    "state": "Juucqrd",
    "value": 1.6831843892751275
  },
  {
    "continent": "Americas",
    "year": 2016,
    "state": "Ktaq np",
    "value": 27.574234534710442
  },
  {
    "continent": "Americas",
    "year": 2016,
    "state": "Xjzxccd",
    "value": 56.92744198752449
  },
  {
    "continent": "Americas",
    "year": 2017,
    "state": "Ktaq np",
    "value": 41.10078504806991
  },
  {
    "continent": "Americas",
    "year": 2017,
    "state": "Xjzxccd",
    "value": 28.56665484963914
  },
  {
    "continent": "Americas",
    "year": 2018,
    "state": "Ktaq np",
    "value": 79.81517223034149
  },
  {
    "continent": "Americas",
    "year": 2018,
    "state": "Xjzxccd",
    "value": 17.274959818275715
  },
  {
    "continent": "Americas",
    "year": 2019,
    "state": "Ktaq np",
    "value": 48.15827138437179
  },
  {
    "continent": "Americas",
    "year": 2019,
    "state": "Xjzxccd",
    "value": 57.19057047246159
  }
]

和各大洲:

const continents = ['Europe', 'Asia', 'Africa', 'Americas']

我以这种方式迭代continents数组:

continents.map((continent, i) => {
   const numOfPreviousStates = ??
})

numOfPreviousStates应该包含当前大陆之前的州数量。

因此,如果continent = 'Europe'然后numOfPreviousStates = 0,如果continent = 'Asia'然后numOfPreviousStates = 0+8=8,如果continent = 'Africa'然后numOfPreviousStates = 8+12=20并且如果continent = 'Americas'然后numOfPreviousStates = 8+12+12 = 32

我该怎么做?我想我可以使用reduce,但是如何使用?

3 个答案:

答案 0 :(得分:3)

您可以先获取计数,然后映射先前的值并保留计数。

const
    data = [{ continent: "Europe" }, { continent: "Europe" }, { continent: "Europe" }, { continent: "Europe" }, { continent: "Europe" }, { continent: "Europe" }, { continent: "Europe" }, { continent: "Europe" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Asia" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Africa" }, { continent: "Americas" }, { continent: "Americas" }, { continent: "Americas" }, { continent: "Americas" }, { continent: "Americas" }, { continent: "Americas" }, { continent: "Americas" }, { continent: "Americas" }],
    continents = ['Europe', 'Asia', 'Africa', 'Americas'],
    counts = data.reduce((c, { continent }) => (c[continent] = (c[continent] || 0) + 1, c), {}),
    result = continents.map((last => c => [last, last += counts[c] || 0][0])(0));
  
console.log(result);

答案 1 :(得分:2)

您可以像这样使用过滤:

continents.map((continent, i) => {
   return data.filter(e => continents.indexOf(e.continent)<i).length;
})

答案 2 :(得分:0)

使用老式循环的粗略但有效的解决方案:

public class SeqIntIdGenerator<TEntity> : IIdGenerator
{
    public static SeqIntIdGenerator<TEntity> Instance { get; } = new SeqIntIdGenerator<TEntity>();

    public object GenerateId(object container, object document)
    {
        //InvalidCastException thrown here on InsertOneAsync!!!
        var idSequenceCollection = ((IMongoCollection<TEntity>)container).Database.GetCollection<dynamic>("Counters");

        var filter = Builders<dynamic>.Filter.Eq("_id", ((IMongoCollection<TEntity>)container).CollectionNamespace.CollectionName);
        var update = Builders<dynamic>.Update.Inc("Seq", 1);

        var options = new FindOneAndUpdateOptions<dynamic>
        {
            IsUpsert = true,
            ReturnDocument = ReturnDocument.After
        };

        return idSequenceCollection.FindOneAndUpdate(filter, update, options).Seq;
    }

    public bool IsEmpty(object id)
    {
        return (int)id == 0;
    }
}