简体   繁体   中英

Count number of previous elements

I have an array of objects like that:

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
  }
]

and an array of continents:

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

I iterate the continents array in this way:

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

numOfPreviousStates should contains the number of the states that come before the current continent.

So, if continent = 'Europe' then numOfPreviousStates = 0 , if continent = 'Asia' then numOfPreviousStates = 0+8=8 , if continent = 'Africa' then numOfPreviousStates = 8+12=20 and if continent = 'Americas' then numOfPreviousStates = 8+12+12 = 32 .

How can I do that? I think I can use reduce but how?

You could get the counts first and then map the previous values and keep the count.

 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); 

You could use filtering like this:

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

A crude-but-effective solution using old-school loops:

let i = 0, j = 0, previousStateCounts = [0,0,0,0];
for(; i < data.length; i++){
    for(; j < continents.length - 1; j++){
        if(continents[j] == data[i].continent){
            previousStateCounts[j+1] = i + 1;
            break;
        }
    }
}
console.log(previousStateCounts);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM