Calculate total number of values from same JSON key - javascript

I want to calculate the total number of cases, recovered and deaths as whole from a JSON object. Suppose for example: the date 3/27/20 should have the value of the computed the total number of cases, recovered and deaths for the remaining countries. How can I do in this JavaScript, and return me an object of computed values for each date, removing the countries, province? I have the following JSON object:
{
"country": "Afghanistan",
"province": null,
"timeline": {
"cases": {
"3/27/20": 110,
"3/28/20": 110,
"3/29/20": 120,
"3/30/20": 170,
"3/31/20": 174,
"4/1/20": 237,
"4/2/20": 273,
"4/3/20": 281,
"4/4/20": 299,
"4/5/20": 349,
"4/6/20": 367,
"4/7/20": 423,
"4/8/20": 444,
"4/9/20": 484,
"4/10/20": 521
},
"deaths": {
"3/27/20": 4,
"3/28/20": 4,
"3/29/20": 4,
"3/30/20": 4,
"3/31/20": 4,
"4/1/20": 4,
"4/2/20": 6,
"4/3/20": 6,
"4/4/20": 7,
"4/5/20": 7,
"4/6/20": 11,
"4/7/20": 14,
"4/8/20": 14,
"4/9/20": 15,
"4/10/20": 15
},
"recovered": {
"3/27/20": 2,
"3/28/20": 2,
"3/29/20": 2,
"3/30/20": 2,
"3/31/20": 5,
"4/1/20": 5,
"4/2/20": 10,
"4/3/20": 10,
"4/4/20": 10,
"4/5/20": 15,
"4/6/20": 18,
"4/7/20": 18,
"4/8/20": 29,
"4/9/20": 32,
"4/10/20": 32
}
}
},
{
"country": "Albania",
"province": null,
"timeline": {
"cases": {
"3/27/20": 186,
"3/28/20": 197,
"3/29/20": 212,
"3/30/20": 223,
"3/31/20": 243,
"4/1/20": 259,
"4/2/20": 277,
"4/3/20": 304,
"4/4/20": 333,
"4/5/20": 361,
"4/6/20": 377,
"4/7/20": 383,
"4/8/20": 400,
"4/9/20": 409,
"4/10/20": 416
},
"deaths": {
"3/27/20": 8,
"3/28/20": 10,
"3/29/20": 10,
"3/30/20": 11,
"3/31/20": 15,
"4/1/20": 15,
"4/2/20": 16,
"4/3/20": 17,
"4/4/20": 20,
"4/5/20": 20,
"4/6/20": 21,
"4/7/20": 22,
"4/8/20": 22,
"4/9/20": 23,
"4/10/20": 23
},
"recovered": {
"3/27/20": 31,
"3/28/20": 31,
"3/29/20": 33,
"3/30/20": 44,
"3/31/20": 52,
"4/1/20": 67,
"4/2/20": 76,
"4/3/20": 89,
"4/4/20": 99,
"4/5/20": 104,
"4/6/20": 116,
"4/7/20": 131,
"4/8/20": 154,
"4/9/20": 165,
"4/10/20": 182
}
}
}

var a = [{"country":"Afghanistan","province":null,"timeline":{"cases":{"3/27/20":110,"3/28/20":110,"3/29/20":120,"3/30/20":170,"3/31/20":174,"4/1/20":237,"4/2/20":273,"4/3/20":281,"4/4/20":299,"4/5/20":349,"4/6/20":367,"4/7/20":423,"4/8/20":444,"4/9/20":484,"4/10/20":521},"deaths":{"3/27/20":4,"3/28/20":4,"3/29/20":4,"3/30/20":4,"3/31/20":4,"4/1/20":4,"4/2/20":6,"4/3/20":6,"4/4/20":7,"4/5/20":7,"4/6/20":11,"4/7/20":14,"4/8/20":14,"4/9/20":15,"4/10/20":15},"recovered":{"3/27/20":2,"3/28/20":2,"3/29/20":2,"3/30/20":2,"3/31/20":5,"4/1/20":5,"4/2/20":10,"4/3/20":10,"4/4/20":10,"4/5/20":15,"4/6/20":18,"4/7/20":18,"4/8/20":29,"4/9/20":32,"4/10/20":32}}},{"country":"Albania","province":null,"timeline":{"cases":{"3/27/20":186,"3/28/20":197,"3/29/20":212,"3/30/20":223,"3/31/20":243,"4/1/20":259,"4/2/20":277,"4/3/20":304,"4/4/20":333,"4/5/20":361,"4/6/20":377,"4/7/20":383,"4/8/20":400,"4/9/20":409,"4/10/20":416},"deaths":{"3/27/20":8,"3/28/20":10,"3/29/20":10,"3/30/20":11,"3/31/20":15,"4/1/20":15,"4/2/20":16,"4/3/20":17,"4/4/20":20,"4/5/20":20,"4/6/20":21,"4/7/20":22,"4/8/20":22,"4/9/20":23,"4/10/20":23},"recovered":{"3/27/20":31,"3/28/20":31,"3/29/20":33,"3/30/20":44,"3/31/20":52,"4/1/20":67,"4/2/20":76,"4/3/20":89,"4/4/20":99,"4/5/20":104,"4/6/20":116,"4/7/20":131,"4/8/20":154,"4/9/20":165,"4/10/20":182}}}]
var result = a.map(function(e){return {cases: e.timeline.cases, deaths: e.timeline.deaths, recovered: e.timeline.recovered};}).reduce(function(acc, e){
Object.keys(e).forEach(function(t){
Object.keys(e[t]).forEach(function(d){
acc[t][d] = (acc[t][d] || 0) + e[t][d];
});
});
return acc;
}, {deaths: {}, recovered: {}, cases: {}});
console.log(result);

Related

Error: Unsupported strips at Object.writeSTL in vtk.js

I am trying to create STL files using vtk.js of a vtkTubeFilter polyData.
The error am getting is:
Error: Unsupported strips
at Object.writeSTL
The code I'm using to get STL data:
const data = vtkSTLWriter.writeSTL(
polyData,
'ascii',
transform.getMatrix()
);
where, transform is a vtkTransform object and polyData is the polyData obtained from getOutputData() on vtkTubeFilter source.
As per vtk.js source code for writeSTL function, the number of strips must be 0 or null.
I checked on the polyData object, it has an array of length 140 for getStrips().getData().
let strips = newPolyData.getStrips().getData();
console.log(strips.length);
console.log(strips)
140
Uint32Array(140) [6, 1, 0, 21, 20, 41, 40, 6, 2, 1, 22, 21, 42, 41, 6, 3, 2, 23, 22, 43, 42, 6, 4, 3, 24, 23, 44, 43, 6, 5, 4, 25, 24, 45, 44, 6, 6, 5, 26, 25, 46, 45, 6, 7, 6, 27, 26, 47, 46, 6, 8, 7, 28, 27, 48, 47, 6, 9, 8, 29, 28, 49, 48, 6, 10, 9, 30, 29, 50, 49, 6, 11, 10, 31, 30, 51, 50, 6, 12, 11, 32, 31, 52, 51, 6, 13, 12, 33, 32, 53, 52, 6, 14, 13, 34, 33, 54, 53, 6, 15, …]
How can fix/debug this error, and still write it to STL file. Or, what am I doing wrong?

Copying subarrays into another array while changing them?

So I'm trying to copy these subarrays into another array and change them so their values are cumulative as I put them in. In order to make them cumulative, I am using the map function and modeling that based on the constant variable I made. In my attempts to copy them over into the other array, I have tried concat, push, and other methods without success.
JS
const cumulative = (cumu => val => cumu += val)(0);
var series2 = series.map(cumulative);
series: [
[55, 65, 76, 88, 44, 33, 54, 65, 7, 98, 12, 109],
[52, 25, 26, 82, 24, 23, 34, 65, 47, 59, 12, 19],
[57, 68, 77, 78, 44, 43, 74, 16, 71, 91, 11, 29]
]
This array below is just to demonstrate the desired effect I am attempting to do...
series2: [
[55, 120, 196... etc...],
[...],
[...]
]
Guess I cracked it. Your logic is wrong there, you should use an inner map:
series = [
[55, 65, 76, 88, 44, 33, 54, 65, 7, 98, 12, 109],
[52, 25, 26, 82, 24, 23, 34, 65, 47, 59, 12, 19],
[57, 68, 77, 78, 44, 43, 74, 16, 71, 91, 11, 29]
];
const cumulative = (cumu) => {
var val = 0;
return cumu.map(c => val += c);
};
var series2 = series.map(cumulative);
console.log(series2);
I get this as output and this is what you're upto:
[
[55, 120, 196, 284, 328, 361, 415, 480, 487, 585, 597, 706],
[758, 783, 809, 891, 915, 938, 972, 1037, 1084, 1143, 1155, 1174],
[1231, 1299, 1376, 1454, 1498, 1541, 1615, 1631, 1702, 1793, 1804, 1833]
]
I think reduce is the best tool to transform an array in the way you want - to one of cumulative sums. Use this to define the function that transforms a single array, then just map it over the outer array:
const series = [
[55, 65, 76, 88, 44, 33, 54, 65, 7, 98, 12, 109],
[52, 25, 26, 82, 24, 23, 34, 65, 47, 59, 12, 19],
[57, 68, 77, 78, 44, 43, 74, 16, 71, 91, 11, 29]
];
const makeCumulative = arr => arr.reduce((cums, current) => {
const subtotal = cums[cums.length - 1] || 0;
return [...cums, subtotal + current];
}, []);
const series2 = series.map(makeCumulative);
console.log(series2);
Using reduce:
let series = [
[55, 65, 76, 88, 44, 33, 54, 65, 7, 98, 12, 109],
[52, 25, 26, 82, 24, 23, 34, 65, 47, 59, 12, 19],
[57, 68, 77, 78, 44, 43, 74, 16, 71, 91, 11, 29],
];
console.log(series.map(
(ar) => ar.reduce((acc, el) => [...acc, el + (acc[acc.length - 1] || 0)], [])
));

Find how many days it takes for a value to double in an object

I'm making a table with the latest number cases of coronavirus in JavaScript, and I am at a point where I want to create a column that shows how many days it took for the number of confirmCases to double. Here is an example:
{
"message": "Success",
"source": "JHU CSSE",
"sourceURL": "https://github.com/CSSEGISandData/2019-nCoV",
"updateDate": "2020-03-22T11:08:41.651Z",
"data": {
"3/15/20": {
"Afghanistan": 16,
"Albania": 42,
"Algeria": 48,
"Andorra": 1,
"Angola": 0,
"Antigua and Barbuda": 1,
"Argentina": 45,
"Armenia": 26,
"Australia": 297,
"Austria": 860,
"Azerbaijan": 23,
"Bahamas, The": 0,
"Bahrain": 214,
"Bangladesh": 5,
"Barbados": 0,
"Belarus": 27,
"Belgium": 886,
"Benin": 0,
"Bhutan": 1,
"Bolivia": 10,
"Bosnia and Herzegovina": 24,
"Brazil": 162,
"Brunei": 50,
"Bulgaria": 51,
"Burkina Faso": 3,
"Cabo Verde": 0,
"Cambodia": 7,
"Cameroon": 2,
"Canada": 252,
"Cape Verde": 0,
"Central African Republic": 1,
"Chad": 0,
"Chile": 74,
"China": 81003,
"Colombia": 34,
"Congo (Brazzaville)": 1,
"Congo (Kinshasa)": 2,
"Costa Rica": 27,
"Cote d'Ivoire": 1,
"Croatia": 49,
"Cruise Ship": 696,
"Cuba": 4,
"Cyprus": 26,
"Czechia": 253,
"Denmark": 875,
"Djibouti": 0,
"Dominican Republic": 11,
"East Timor": 0,
"Ecuador": 28,
"Egypt": 110,
"El Salvador": 0,
"Equatorial Guinea": 1,
"Eritrea": 0,
"Estonia": 171,
"Eswatini": 1,
"Ethiopia": 1,
"Fiji": 0,
"Finland": 244,
"France": 4523,
"Gabon": 1,
"Gambia, The": 0,
"Georgia": 33,
"Germany": 5795,
"Ghana": 6,
"Greece": 331,
"Guatemala": 1,
"Guinea": 1,
"Guyana": 4,
"Haiti": 0,
"Holy See": 1,
"Honduras": 3,
"Hungary": 32,
"Iceland": 171,
"India": 113,
"Indonesia": 117,
"Iran": 13938,
"Iraq": 116,
"Ireland": 129,
"Israel": 251,
"Italy": 24747,
"Jamaica": 10,
"Japan": 839,
"Jordan": 8,
"Kazakhstan": 9,
"Kenya": 3,
"Korea, South": 8162,
"Kosovo": 2,
"Kuwait": 112,
"Kyrgyzstan": 0,
"Latvia": 30,
"Lebanon": 110,
"Liberia": 0,
"Liechtenstein": 4,
"Lithuania": 12,
"Luxembourg": 59,
"Madagascar": 0,
"Malaysia": 428,
"Maldives": 13,
"Malta": 21,
"Martinique": 9,
"Mauritania": 1,
"Mauritius": 0,
"Mexico": 41,
"Moldova": 23,
"Monaco": 2,
"Mongolia": 1,
"Montenegro": 0,
"Morocco": 28,
"Namibia": 2,
"Nepal": 1,
"Netherlands": 1138,
"New Zealand": 8,
"Nicaragua": 0,
"Niger": 0,
"Nigeria": 2,
"North Macedonia": 14,
"Norway": 1221,
"Oman": 22,
"Pakistan": 53,
"Panama": 43,
"Papua New Guinea": 0,
"Paraguay": 6,
"Peru": 43,
"Philippines": 140,
"Poland": 119,
"Portugal": 245,
"Qatar": 401,
"Romania": 131,
"Russia": 63,
"Rwanda": 1,
"Saint Lucia": 2,
"Saint Vincent and the Grenadines": 1,
"San Marino": 101,
"Saudi Arabia": 103,
"Senegal": 24,
"Serbia": 48,
"Seychelles": 2,
"Singapore": 226,
"Slovakia": 54,
"Slovenia": 219,
"Somalia": 0,
"South Africa": 51,
"Spain": 7798,
"Sri Lanka": 18,
"Sudan": 1,
"Suriname": 1,
"Sweden": 1022,
"Switzerland": 2200,
"Taiwan*": 59,
"Tanzania": 0,
"Thailand": 114,
"Togo": 1,
"Trinidad and Tobago": 2,
"Tunisia": 18,
"Turkey": 6,
"US": 3499,
"Uganda": 0,
"Ukraine": 3,
"United Arab Emirates": 98,
"United Kingdom": 1145,
"Uruguay": 4,
"Uzbekistan": 1,
"Venezuela": 10,
"Vietnam": 56,
"Zambia": 0,
"Zimbabwe": 0
},
"3/16/20": {
"Afghanistan": 21,
"Albania": 51,
"Algeria": 54,
"Andorra": 2,
"Angola": 0,
"Antigua and Barbuda": 1,
"Argentina": 56,
"Armenia": 52,
"Australia": 377,
"Austria": 1018,
"Azerbaijan": 15,
"Bahamas, The": 1,
"Bahrain": 214,
"Bangladesh": 8,
"Barbados": 0,
"Belarus": 36,
"Belgium": 1058,
"Benin": 1,
"Bhutan": 1,
"Bolivia": 11,
"Bosnia and Herzegovina": 25,
"Brazil": 200,
"Brunei": 54,
"Bulgaria": 52,
"Burkina Faso": 15,
"Cabo Verde": 0,
"Cambodia": 7,
"Cameroon": 4,
"Canada": 415,
"Cape Verde": 0,
"Central African Republic": 1,
"Chad": 0,
"Chile": 155,
"China": 81033,
"Colombia": 54,
"Congo (Brazzaville)": 1,
"Congo (Kinshasa)": 2,
"Costa Rica": 35,
"Cote d'Ivoire": 1,
"Croatia": 57,
"Cruise Ship": 696,
"Cuba": 4,
"Cyprus": 33,
"Czechia": 298,
"Denmark": 933,
"Djibouti": 0,
"Dominican Republic": 11,
"East Timor": 0,
"Ecuador": 37,
"Egypt": 150,
"El Salvador": 0,
"Equatorial Guinea": 1,
"Eritrea": 0,
"Estonia": 205,
"Eswatini": 1,
"Ethiopia": 5,
"Fiji": 0,
"Finland": 277,
"France": 6668,
"Gabon": 1,
"Gambia, The": 0,
"Georgia": 33,
"Germany": 7272,
"Ghana": 6,
"Greece": 331,
"Guatemala": 2,
"Guinea": 1,
"Guyana": 4,
"Haiti": 0,
"Holy See": 1,
"Honduras": 6,
"Hungary": 39,
"Iceland": 180,
"India": 119,
"Indonesia": 134,
"Iran": 14991,
"Iraq": 124,
"Ireland": 169,
"Israel": 255,
"Italy": 27980,
"Jamaica": 10,
"Japan": 825,
"Jordan": 17,
"Kazakhstan": 10,
"Kenya": 3,
"Korea, South": 8236,
"Kosovo": 2,
"Kuwait": 123,
"Kyrgyzstan": 0,
"Latvia": 34,
"Lebanon": 99,
"Liberia": 1,
"Liechtenstein": 4,
"Lithuania": 17,
"Luxembourg": 77,
"Madagascar": 0,
"Malaysia": 566,
"Maldives": 13,
"Malta": 30,
"Martinique": 15,
"Mauritania": 1,
"Mauritius": 0,
"Mexico": 53,
"Moldova": 23,
"Monaco": 7,
"Mongolia": 1,
"Montenegro": 0,
"Morocco": 29,
"Namibia": 2,
"Nepal": 1,
"Netherlands": 1416,
"New Zealand": 8,
"Nicaragua": 0,
"Niger": 0,
"Nigeria": 2,
"North Macedonia": 18,
"Norway": 1333,
"Oman": 22,
"Pakistan": 136,
"Panama": 55,
"Papua New Guinea": 0,
"Paraguay": 8,
"Peru": 86,
"Philippines": 142,
"Poland": 177,
"Portugal": 331,
"Qatar": 439,
"Romania": 158,
"Russia": 90,
"Rwanda": 5,
"Saint Lucia": 2,
"Saint Vincent and the Grenadines": 1,
"San Marino": 109,
"Saudi Arabia": 118,
"Senegal": 24,
"Serbia": 55,
"Seychelles": 3,
"Singapore": 243,
"Slovakia": 63,
"Slovenia": 253,
"Somalia": 1,
"South Africa": 62,
"Spain": 9942,
"Sri Lanka": 28,
"Sudan": 1,
"Suriname": 1,
"Sweden": 1103,
"Switzerland": 2200,
"Taiwan*": 67,
"Tanzania": 1,
"Thailand": 147,
"Togo": 1,
"Trinidad and Tobago": 4,
"Tunisia": 20,
"Turkey": 18,
"US": 4632,
"Uganda": 0,
"Ukraine": 7,
"United Arab Emirates": 98,
"United Kingdom": 1551,
"Uruguay": 8,
"Uzbekistan": 6,
"Venezuela": 17,
"Vietnam": 61,
"Zambia": 0,
"Zimbabwe": 0
},
"3/17/20": {
"Afghanistan": 22,
"Albania": 55,
"Algeria": 60,
"Andorra": 39,
"Angola": 0,
"Antigua and Barbuda": 1,
"Argentina": 68,
"Armenia": 78,
"Australia": 452,
"Austria": 1332,
"Azerbaijan": 28,
"Bahamas, The": 1,
"Bahrain": 228,
"Bangladesh": 10,
"Barbados": 2,
"Belarus": 36,
"Belgium": 1243,
"Benin": 1,
"Bhutan": 1,
"Bolivia": 11,
"Bosnia and Herzegovina": 26,
"Brazil": 321,
"Brunei": 56,
"Bulgaria": 67,
"Burkina Faso": 15,
"Cabo Verde": 0,
"Cambodia": 33,
"Cameroon": 10,
"Canada": 478,
"Cape Verde": 0,
"Central African Republic": 1,
"Chad": 0,
"Chile": 201,
"China": 81058,
"Colombia": 65,
"Congo (Brazzaville)": 1,
"Congo (Kinshasa)": 3,
"Costa Rica": 41,
"Cote d'Ivoire": 5,
"Croatia": 65,
"Cruise Ship": 696,
"Cuba": 5,
"Cyprus": 46,
"Czechia": 396,
"Denmark": 1025,
"Djibouti": 0,
"Dominican Republic": 21,
"East Timor": 0,
"Ecuador": 58,
"Egypt": 196,
"El Salvador": 0,
"Equatorial Guinea": 1,
"Eritrea": 0,
"Estonia": 225,
"Eswatini": 1,
"Ethiopia": 5,
"Fiji": 0,
"Finland": 321,
"France": 7699,
"Gabon": 1,
"Gambia, The": 1,
"Georgia": 34,
"Germany": 9257,
"Ghana": 7,
"Greece": 387,
"Guatemala": 6,
"Guinea": 1,
"Guyana": 7,
"Haiti": 0,
"Holy See": 1,
"Honduras": 8,
"Hungary": 50,
"Iceland": 220,
"India": 142,
"Indonesia": 172,
"Iran": 16169,
"Iraq": 154,
"Ireland": 223,
"Israel": 337,
"Italy": 31506,
"Jamaica": 12,
"Japan": 878,
"Jordan": 34,
"Kazakhstan": 33,
"Kenya": 3,
"Korea, South": 8320,
"Kosovo": 2,
"Kuwait": 130,
"Kyrgyzstan": 0,
"Latvia": 49,
"Lebanon": 120,
"Liberia": 1,
"Liechtenstein": 7,
"Lithuania": 25,
"Luxembourg": 140,
"Madagascar": 0,
"Malaysia": 673,
"Maldives": 13,
"Malta": 38,
"Martinique": 16,
"Mauritania": 1,
"Mauritius": 0,
"Mexico": 82,
"Moldova": 30,
"Monaco": 7,
"Mongolia": 5,
"Montenegro": 2,
"Morocco": 38,
"Namibia": 2,
"Nepal": 1,
"Netherlands": 1711,
"New Zealand": 12,
"Nicaragua": 0,
"Niger": 0,
"Nigeria": 3,
"North Macedonia": 26,
"Norway": 1463,
"Oman": 24,
"Pakistan": 236,
"Panama": 69,
"Papua New Guinea": 0,
"Paraguay": 9,
"Peru": 117,
"Philippines": 187,
"Poland": 238,
"Portugal": 448,
"Qatar": 439,
"Romania": 184,
"Russia": 114,
"Rwanda": 7,
"Saint Lucia": 2,
"Saint Vincent and the Grenadines": 1,
"San Marino": 109,
"Saudi Arabia": 171,
"Senegal": 26,
"Serbia": 65,
"Seychelles": 4,
"Singapore": 266,
"Slovakia": 72,
"Slovenia": 275,
"Somalia": 1,
"South Africa": 62,
"Spain": 11748,
"Sri Lanka": 44,
"Sudan": 1,
"Suriname": 1,
"Sweden": 1190,
"Switzerland": 2700,
"Taiwan*": 77,
"Tanzania": 1,
"Thailand": 177,
"Togo": 1,
"Trinidad and Tobago": 5,
"Tunisia": 24,
"Turkey": 47,
"US": 6421,
"Uganda": 0,
"Ukraine": 14,
"United Arab Emirates": 98,
"United Kingdom": 1960,
"Uruguay": 29,
"Uzbekistan": 10,
"Venezuela": 33,
"Vietnam": 66,
"Zambia": 0,
"Zimbabwe": 0
},
"3/18/20": {
"Afghanistan": 22,
"Albania": 59,
"Algeria": 74,
"Andorra": 39,
"Angola": 0,
"Antigua and Barbuda": 1,
"Argentina": 79,
"Armenia": 84,
"Australia": 568,
"Austria": 1646,
"Azerbaijan": 28,
"Bahamas, The": 1,
"Bahrain": 256,
"Bangladesh": 14,
"Barbados": 2,
"Belarus": 51,
"Belgium": 1486,
"Benin": 2,
"Bhutan": 1,
"Bolivia": 12,
"Bosnia and Herzegovina": 38,
"Brazil": 372,
"Brunei": 68,
"Bulgaria": 92,
"Burkina Faso": 20,
"Cabo Verde": 0,
"Cambodia": 35,
"Cameroon": 10,
"Canada": 657,
"Cape Verde": 0,
"Central African Republic": 1,
"Chad": 0,
"Chile": 238,
"China": 81102,
"Colombia": 93,
"Congo (Brazzaville)": 1,
"Congo (Kinshasa)": 4,
"Costa Rica": 50,
"Cote d'Ivoire": 6,
"Croatia": 81,
"Cruise Ship": 712,
"Cuba": 7,
"Cyprus": 49,
"Czechia": 464,
"Denmark": 1116,
"Djibouti": 1,
"Dominican Republic": 21,
"East Timor": 0,
"Ecuador": 111,
"Egypt": 196,
"El Salvador": 0,
"Equatorial Guinea": 4,
"Eritrea": 0,
"Estonia": 258,
"Eswatini": 1,
"Ethiopia": 6,
"Fiji": 0,
"Finland": 336,
"France": 9105,
"Gabon": 1,
"Gambia, The": 1,
"Georgia": 38,
"Germany": 12327,
"Ghana": 7,
"Greece": 418,
"Guatemala": 6,
"Guinea": 1,
"Guyana": 7,
"Haiti": 0,
"Holy See": 1,
"Honduras": 9,
"Hungary": 58,
"Iceland": 250,
"India": 156,
"Indonesia": 227,
"Iran": 17361,
"Iraq": 164,
"Ireland": 292,
"Israel": 433,
"Italy": 35713,
"Jamaica": 13,
"Japan": 889,
"Jordan": 52,
"Kazakhstan": 35,
"Kenya": 3,
"Korea, South": 8413,
"Kosovo": 2,
"Kuwait": 142,
"Kyrgyzstan": 3,
"Latvia": 71,
"Lebanon": 133,
"Liberia": 2,
"Liechtenstein": 28,
"Lithuania": 27,
"Luxembourg": 203,
"Madagascar": 0,
"Malaysia": 790,
"Maldives": 13,
"Malta": 38,
"Martinique": 19,
"Mauritania": 1,
"Mauritius": 3,
"Mexico": 93,
"Moldova": 30,
"Monaco": 7,
"Mongolia": 6,
"Montenegro": 1,
"Morocco": 49,
"Namibia": 2,
"Nepal": 1,
"Netherlands": 2058,
"New Zealand": 20,
"Nicaragua": 0,
"Niger": 0,
"Nigeria": 8,
"North Macedonia": 35,
"Norway": 1550,
"Oman": 39,
"Pakistan": 299,
"Panama": 86,
"Papua New Guinea": 0,
"Paraguay": 11,
"Peru": 145,
"Philippines": 202,
"Poland": 251,
"Portugal": 448,
"Qatar": 452,
"Romania": 260,
"Russia": 147,
"Rwanda": 8,
"Saint Lucia": 2,
"Saint Vincent and the Grenadines": 1,
"San Marino": 119,
"Saudi Arabia": 171,
"Senegal": 31,
"Serbia": 83,
"Seychelles": 4,
"Singapore": 313,
"Slovakia": 105,
"Slovenia": 275,
"Somalia": 1,
"South Africa": 116,
"Spain": 13910,
"Sri Lanka": 51,
"Sudan": 2,
"Suriname": 1,
"Sweden": 1279,
"Switzerland": 3028,
"Taiwan*": 100,
"Tanzania": 3,
"Thailand": 212,
"Togo": 1,
"Trinidad and Tobago": 7,
"Tunisia": 29,
"Turkey": 98,
"US": 7783,
"Uganda": 0,
"Ukraine": 14,
"United Arab Emirates": 113,
"United Kingdom": 2642,
"Uruguay": 50,
"Uzbekistan": 15,
"Venezuela": 36,
"Vietnam": 75,
"Zambia": 2,
"Zimbabwe": 0
},
"3/19/20": {
"Afghanistan": 22,
"Albania": 64,
"Algeria": 87,
"Andorra": 53,
"Angola": 0,
"Antigua and Barbuda": 1,
"Argentina": 97,
"Armenia": 115,
"Australia": 681,
"Austria": 2013,
"Azerbaijan": 44,
"Bahamas, The": 3,
"Bahrain": 278,
"Bangladesh": 17,
"Barbados": 5,
"Belarus": 51,
"Belgium": 1795,
"Benin": 2,
"Bhutan": 1,
"Bolivia": 12,
"Bosnia and Herzegovina": 63,
"Brazil": 621,
"Brunei": 75,
"Bulgaria": 94,
"Burkina Faso": 33,
"Cabo Verde": 0,
"Cambodia": 37,
"Cameroon": 13,
"Canada": 800,
"Cape Verde": 0,
"Central African Republic": 1,
"Chad": 1,
"Chile": 238,
"China": 81156,
"Colombia": 102,
"Congo (Brazzaville)": 3,
"Congo (Kinshasa)": 14,
"Costa Rica": 69,
"Cote d'Ivoire": 9,
"Croatia": 105,
"Cruise Ship": 712,
"Cuba": 11,
"Cyprus": 67,
"Czechia": 694,
"Denmark": 1225,
"Djibouti": 1,
"Dominican Republic": 34,
"East Timor": 0,
"Ecuador": 199,
"Egypt": 256,
"El Salvador": 1,
"Equatorial Guinea": 6,
"Eritrea": 0,
"Estonia": 267,
"Eswatini": 1,
"Ethiopia": 6,
"Fiji": 1,
"Finland": 400,
"France": 10947,
"Gabon": 1,
"Gambia, The": 1,
"Georgia": 40,
"Germany": 15320,
"Ghana": 11,
"Greece": 418,
"Guatemala": 9,
"Guinea": 1,
"Guyana": 7,
"Haiti": 0,
"Holy See": 1,
"Honduras": 12,
"Hungary": 73,
"Iceland": 330,
"India": 194,
"Indonesia": 311,
"Iran": 18407,
"Iraq": 192,
"Ireland": 557,
"Israel": 677,
"Italy": 41035,
"Jamaica": 15,
"Japan": 924,
"Jordan": 69,
"Kazakhstan": 44,
"Kenya": 7,
"Korea, South": 8565,
"Kosovo": 2,
"Kuwait": 148,
"Kyrgyzstan": 3,
"Latvia": 86,
"Lebanon": 157,
"Liberia": 2,
"Liechtenstein": 28,
"Lithuania": 36,
"Luxembourg": 335,
"Madagascar": 0,
"Malaysia": 900,
"Maldives": 13,
"Malta": 53,
"Martinique": 23,
"Mauritania": 2,
"Mauritius": 3,
"Mexico": 118,
"Moldova": 49,
"Monaco": 7,
"Mongolia": 6,
"Montenegro": 3,
"Morocco": 63,
"Namibia": 3,
"Nepal": 1,
"Netherlands": 2467,
"New Zealand": 28,
"Nicaragua": 1,
"Niger": 0,
"Nigeria": 8,
"North Macedonia": 48,
"Norway": 1746,
"Oman": 48,
"Pakistan": 454,
"Panama": 109,
"Papua New Guinea": 0,
"Paraguay": 11,
"Peru": 234,
"Philippines": 217,
"Poland": 355,
"Portugal": 785,
"Qatar": 460,
"Romania": 277,
"Russia": 199,
"Rwanda": 8,
"Saint Lucia": 2,
"Saint Vincent and the Grenadines": 1,
"San Marino": 119,
"Saudi Arabia": 274,
"Senegal": 31,
"Serbia": 103,
"Seychelles": 6,
"Singapore": 345,
"Slovakia": 123,
"Slovenia": 286,
"Somalia": 1,
"South Africa": 150,
"Spain": 17963,
"Sri Lanka": 60,
"Sudan": 2,
"Suriname": 1,
"Sweden": 1439,
"Switzerland": 4075,
"Taiwan*": 108,
"Tanzania": 6,
"Thailand": 272,
"Togo": 1,
"Trinidad and Tobago": 9,
"Tunisia": 39,
"Turkey": 192,
"US": 13677,
"Uganda": 0,
"Ukraine": 16,
"United Arab Emirates": 140,
"United Kingdom": 2716,
"Uruguay": 79,
"Uzbekistan": 23,
"Venezuela": 42,
"Vietnam": 85,
"Zambia": 2,
"Zimbabwe": 0
},
"3/20/20": {
"Afghanistan": 24,
"Albania": 70,
"Algeria": 90,
"Andorra": 75,
"Angola": 1,
"Antigua and Barbuda": 1,
"Argentina": 128,
"Armenia": 136,
"Australia": 791,
"Austria": 2388,
"Azerbaijan": 44,
"Bahamas, The": 3,
"Bahrain": 285,
"Bangladesh": 20,
"Barbados": 5,
"Belarus": 69,
"Belgium": 2257,
"Benin": 2,
"Bhutan": 2,
"Bolivia": 15,
"Bosnia and Herzegovina": 89,
"Brazil": 793,
"Brunei": 78,
"Bulgaria": 127,
"Burkina Faso": 40,
"Cabo Verde": 1,
"Cambodia": 51,
"Cameroon": 20,
"Canada": 943,
"Cape Verde": 0,
"Central African Republic": 3,
"Chad": 1,
"Chile": 434,
"China": 81250,
"Colombia": 128,
"Congo (Brazzaville)": 3,
"Congo (Kinshasa)": 18,
"Costa Rica": 89,
"Cote d'Ivoire": 9,
"Croatia": 128,
"Cruise Ship": 712,
"Cuba": 16,
"Cyprus": 67,
"Czechia": 833,
"Denmark": 1337,
"Djibouti": 1,
"Dominican Republic": 72,
"East Timor": 0,
"Ecuador": 367,
"Egypt": 285,
"El Salvador": 1,
"Equatorial Guinea": 6,
"Eritrea": 0,
"Estonia": 283,
"Eswatini": 1,
"Ethiopia": 9,
"Fiji": 1,
"Finland": 450,
"France": 12726,
"Gabon": 3,
"Gambia, The": 1,
"Georgia": 43,
"Germany": 19848,
"Ghana": 16,
"Greece": 495,
"Guatemala": 12,
"Guinea": 1,
"Guyana": 7,
"Haiti": 2,
"Holy See": 1,
"Honduras": 24,
"Hungary": 85,
"Iceland": 409,
"India": 244,
"Indonesia": 369,
"Iran": 19644,
"Iraq": 208,
"Ireland": 683,
"Israel": 705,
"Italy": 47021,
"Jamaica": 16,
"Japan": 963,
"Jordan": 85,
"Kazakhstan": 49,
"Kenya": 7,
"Korea, South": 8652,
"Kosovo": 2,
"Kuwait": 159,
"Kyrgyzstan": 6,
"Latvia": 111,
"Lebanon": 163,
"Liberia": 2,
"Liechtenstein": 28,
"Lithuania": 49,
"Luxembourg": 484,
"Madagascar": 3,
"Malaysia": 1030,
"Maldives": 13,
"Malta": 64,
"Martinique": 32,
"Mauritania": 2,
"Mauritius": 12,
"Mexico": 164,
"Moldova": 66,
"Monaco": 11,
"Mongolia": 6,
"Montenegro": 14,
"Morocco": 77,
"Namibia": 3,
"Nepal": 1,
"Netherlands": 3003,
"New Zealand": 39,
"Nicaragua": 1,
"Niger": 1,
"Nigeria": 12,
"North Macedonia": 67,
"Norway": 1914,
"Oman": 48,
"Pakistan": 501,
"Panama": 137,
"Papua New Guinea": 1,
"Paraguay": 13,
"Peru": 234,
"Philippines": 230,
"Poland": 425,
"Portugal": 1020,
"Qatar": 470,
"Romania": 308,
"Russia": 253,
"Rwanda": 17,
"Saint Lucia": 2,
"Saint Vincent and the Grenadines": 1,
"San Marino": 144,
"Saudi Arabia": 344,
"Senegal": 38,
"Serbia": 135,
"Seychelles": 7,
"Singapore": 385,
"Slovakia": 137,
"Slovenia": 341,
"Somalia": 1,
"South Africa": 202,
"Spain": 20410,
"Sri Lanka": 73,
"Sudan": 2,
"Suriname": 4,
"Sweden": 1639,
"Switzerland": 5294,
"Taiwan*": 135,
"Tanzania": 6,
"Thailand": 322,
"Togo": 9,
"Trinidad and Tobago": 9,
"Tunisia": 54,
"Turkey": 359,
"US": 19100,
"Uganda": 0,
"Ukraine": 29,
"United Arab Emirates": 140,
"United Kingdom": 4014,
"Uruguay": 94,
"Uzbekistan": 33,
"Venezuela": 42,
"Vietnam": 91,
"Zambia": 2,
"Zimbabwe": 1
},
"3/21/20": {
"Afghanistan": 24,
"Albania": 76,
"Algeria": 139,
"Andorra": 88,
"Angola": 2,
"Antigua and Barbuda": 1,
"Argentina": 158,
"Armenia": 160,
"Australia": 1071,
"Austria": 2814,
"Azerbaijan": 53,
"Bahamas, The": 4,
"Bahrain": 305,
"Bangladesh": 25,
"Barbados": 6,
"Belarus": 76,
"Belgium": 2815,
"Benin": 2,
"Bhutan": 2,
"Bolivia": 19,
"Bosnia and Herzegovina": 93,
"Brazil": 1021,
"Brunei": 83,
"Bulgaria": 163,
"Burkina Faso": 64,
"Cabo Verde": 3,
"Cambodia": 53,
"Cameroon": 27,
"Canada": 1278,
"Cape Verde": 1,
"Central African Republic": 3,
"Chad": 1,
"Chile": 537,
"China": 81305,
"Colombia": 196,
"Congo (Brazzaville)": 3,
"Congo (Kinshasa)": 23,
"Costa Rica": 117,
"Cote d'Ivoire": 14,
"Croatia": 206,
"Cruise Ship": 712,
"Cuba": 21,
"Cyprus": 84,
"Czechia": 995,
"Denmark": 1420,
"Djibouti": 1,
"Dominican Republic": 112,
"East Timor": 1,
"Ecuador": 506,
"Egypt": 294,
"El Salvador": 3,
"Equatorial Guinea": 6,
"Eritrea": 1,
"Estonia": 306,
"Eswatini": 1,
"Ethiopia": 9,
"Fiji": 1,
"Finland": 523,
"France": 14431,
"Gabon": 4,
"Gambia, The": 1,
"Georgia": 49,
"Germany": 22213,
"Ghana": 19,
"Greece": 530,
"Guatemala": 17,
"Guinea": 2,
"Guyana": 7,
"Haiti": 2,
"Holy See": 1,
"Honduras": 24,
"Hungary": 103,
"Iceland": 473,
"India": 330,
"Indonesia": 450,
"Iran": 20610,
"Iraq": 214,
"Ireland": 785,
"Israel": 883,
"Italy": 53578,
"Jamaica": 16,
"Japan": 1007,
"Jordan": 85,
"Kazakhstan": 53,
"Kenya": 7,
"Korea, South": 8799,
"Kosovo": 2,
"Kuwait": 176,
"Kyrgyzstan": 14,
"Latvia": 124,
"Lebanon": 187,
"Liberia": 3,
"Liechtenstein": 37,
"Lithuania": 83,
"Luxembourg": 670,
"Madagascar": 3,
"Malaysia": 1183,
"Maldives": 13,
"Malta": 73,
"Martinique": 32,
"Mauritania": 2,
"Mauritius": 14,
"Mexico": 203,
"Moldova": 80,
"Monaco": 11,
"Mongolia": 10,
"Montenegro": 14,
"Morocco": 96,
"Namibia": 3,
"Nepal": 1,
"Netherlands": 3640,
"New Zealand": 52,
"Nicaragua": 2,
"Niger": 1,
"Nigeria": 22,
"North Macedonia": 85,
"Norway": 2118,
"Oman": 52,
"Pakistan": 730,
"Panama": 200,
"Papua New Guinea": 1,
"Paraguay": 18,
"Peru": 318,
"Philippines": 307,
"Poland": 536,
"Portugal": 1280,
"Qatar": 481,
"Romania": 367,
"Russia": 306,
"Rwanda": 17,
"Saint Lucia": 2,
"Saint Vincent and the Grenadines": 1,
"San Marino": 144,
"Saudi Arabia": 392,
"Senegal": 47,
"Serbia": 171,
"Seychelles": 7,
"Singapore": 432,
"Slovakia": 178,
"Slovenia": 383,
"Somalia": 1,
"South Africa": 240,
"Spain": 25374,
"Sri Lanka": 77,
"Sudan": 2,
"Suriname": 4,
"Sweden": 1763,
"Switzerland": 6575,
"Taiwan*": 153,
"Tanzania": 6,
"Thailand": 411,
"Togo": 16,
"Trinidad and Tobago": 49,
"Tunisia": 60,
"Turkey": 670,
"US": 25489,
"Uganda": 1,
"Ukraine": 47,
"United Arab Emirates": 153,
"United Kingdom": 5067,
"Uruguay": 110,
"Uzbekistan": 43,
"Venezuela": 70,
"Vietnam": 94,
"Zambia": 2,
"Zimbabwe": 3
}
}
}
Is it possible for it to return an object where it shows how many days it took for the number of cases in each country to double ?
Maintain the initial count of infected people country wise and then iterate over the countries and check if the count is double of initial count.
// Store number of days
let days = 0;
const countriesWithDoubleDayCount = {};
const dates = Object.keys(data.data);
const countries = Object.values(data.data);
// Create any Object of countries with count set to zero
const groupByCountries = Object.keys(data.data["3/15/20"]).reduce((accu, name) => {
accu[name] = 0;
return accu;
}, {});
const initialCountCountryWise = {};
const doubleCount = countries.reduce((a, country, i) => {
if (i < dates.length - 1) {
// Date Difference Calculation
days += Math.abs((new Date(dates[i + 1]) - new Date(dates[i])) / (1000 * 60 * 60 * 24), 10);
}
const countries = Object.entries(country);
countries.forEach(([name, count]) => {
// First Record
if (i == 0) {
// Maintain the initial count of coronavirus infected people, country wise.
initialCountCountryWise[name] = count !== 0 ? count : 1;
}
a[name] = count;
const double = initialCountCountryWise[name] * 2;
// Check if current country count is greater than double of initial count
// If yes, push in the countriesWithDoubleDayCount Object.
if (a[name] >= double && !countriesWithDoubleDayCount.hasOwnProperty(name)) {
countriesWithDoubleDayCount[name] = days;
}
});
return a;
}, groupByCountries);
console.log(countriesWithDoubleDayCount);
Note - Due to character limit cannot add Javascript live code snippet.

Join first two elements and create new array?

After playing around with the join() and slice() functions for hours, I have just found that you can't use either function on complex arrays. So I've come to get some help here.
I'm trying to get my data below:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
To look like this (output):
var result = [
["North: Tennis", 37, 25, 11, 9, 42, 13],
["East: Football", 41, 2, 3, 26, 47, 21],
["South: Rugby", 7, 22, 35, 45, 11, 46],
["West: Rugby", 30, 21, 44, 23, 4, 47],
["North East: Football", 35, 27, 12, 39, 34, 13],
["North West: Football", 23, 4, 41, 35, 9, 47]
];
Any help would be appreciated
As you want to turn one array into another array, it makes sense to .map the original array to another array by taking the first two elements of each subarray and concatenating them together, then building a new subarray by adding the rest of the elements (which we get with the rest parameter during the array destructuring) to a new array that contains the joined string.
let data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
let result = data.map(([s1, s2, ...rest]) => [`${s1}: ${s2}`, ...rest]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
References:
Array.prototype.map()
Array destructuring
Template literals
If I understand your question correctly, you are looking for the following output. I've used JavaScript's .map() function to restructure the data slightly. More information on .map here.
var data = [
["North", "Tennis", 37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
const formattedData = data.map(row => {
return [
[row[0] + ': ' + row[1]],
row[2],
row[3],
row[4],
row[5],
row[6],
row[7]
];
});
console.log(formattedData);
I'd write your code.
var data = [
["North", "Tennis", 37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
// Final resulting array is created by concatenating the first and
// second index elements of each inner array of data. The rest data
// is being copied as it is using slice and spread operator.
const result = data.reduce(
(mem, cur) => [...mem, [`${cur[0]}: ${cur[1]}`, ...cur.slice(2)]],
[]
);
console.log(result);
You could use map method. It takes a callback which is executed for every element. It then returns a new array with the new data in each index. For example:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
newData = data.map((el, i, arr) => {
el[0] = `${el[0]}: ${el[1]}` // reassinging el[0]
el.splice(1, 1); // removing 2 array element
});

Replacing all elements in a nested Array

I have a challenge that I have been battling with for some time now. It's about replacing all the elements in an array that is in a grid form, but my solution is only replacing selected element of its choice not as I intended.
In this challenge, I want to replace the integer value that is divisible by two with the string "even" while the rest replaced with the string "odd".
/*
* - The numbers variable is an array of arrays.
* - a nested for loop to cycle through numbers.
* - it convert each even number to the string "even"
* - and Convert each odd number to the string "odd"
*/
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for(var row=0; row<myNumbers.length; row++) {
for(var column=0;column<myNumbers[row].length;column++) {
if(myNumbers[column]%2===0){
myNumbers[column].splice(column,1,"even");
}else{
myNumbers[column].splice(column,1,"odd");
}
console.log(myNumbers[row][column]);
}
}
Code Output:


odd
12
23
12
45
45
78
66
223
3
34
odd
1
553
23
4
66
23
4
55
67
56
odd
553
44
55
5
428
452
3
12
31
55
odd
79
44
674
224
4
21
4
2
3
52
odd
51
44
1
67
5
5
65
4
5
5
odd
5
43
23
4424
74
532
6
7
35
17
odd
43
43
66
53
6
89
10
23
52
111
odd
109
80
67
6
53
537
2
168
16
2
odd
8
76
7
9
6
3
73
77
100
56
odd
You used the wrong variable for the row index
myNumbers[column]
//needs to be
myNumbers[row]
Also your if condition is using the wrong row index, and trying to compare against the whole array instead of the value in the array
if(myNumbers[column]%2===0)
//needs to be
if(myNumbers[row][column]%2===0)
Demo
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for (var row = 0; row < myNumbers.length; row++) {
for (var column = 0; column < myNumbers[row].length; column++) {
if (myNumbers[row][column] % 2 === 0) {
myNumbers[row].splice(column, 1, "even");
} else {
myNumbers[row].splice(column, 1, "odd");
}
}
}
console.log(myNumbers);
Probably easier to use a nested map instead:
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
const output = myNumbers.map(row => row.map(num =>
num % 2 === 0
? 'even'
: 'odd'
));
console.log(output);
Achieving the same thing with a for loop is much more verbose and confusing, and shouldn't be done in most cases (array methods have better abstraction and don't require manual iteration), but if necessary:
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
const output = [];
for (let rowIndex = 0; rowIndex < myNumbers.length; rowIndex++) {
const row = myNumbers[rowIndex];
const newRow = [];
for (let colIndex = 0; colIndex < row.length; colIndex++) {
const num = row[colIndex];
newRow.push(num % 2 === 0 ? 'even' : 'odd');
}
output.push(newRow);
}
console.log(output);
Why not simply set the value of myNumbers[row][column] instead of using splice?
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for(var row=0; row<myNumbers.length; row++) {
for(var column=0;column<myNumbers[row].length;column++) {
if(myNumbers[row][column]%2===0) {
myNumbers[row][column] = "even";
} else{
myNumbers[row][column] = "odd";
}
console.log(myNumbers[row][column]);
}
}
In your inner loop you need to access the values using both indexes (i.e. myNumbers[row][column]). As it stands, you are only using the column index and therefore splicing values into the array containing the rows.

Categories

Resources