[JS30] -4) Array Cardio Day 1
data
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
{ first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];
const people = [
'Bernhard, Sandra', 'Bethea, Erin', 'Becker, Carl', 'Bentsen, Lloyd', 'Beckett, Samuel', 'Blake, William', 'Berger, Ric', 'Beddoes, Mick', 'Beethoven, Ludwig',
'Belloc, Hilaire', 'Begin, Menachem', 'Bellow, Saul', 'Benchley, Robert', 'Blair, Robert', 'Benenson, Peter', 'Benjamin, Walter', 'Berlin, Irving',
'Benn, Tony', 'Benson, Leana', 'Bent, Silas', 'Berle, Milton', 'Berry, Halle', 'Biko, Steve', 'Beck, Glenn', 'Bergman, Ingmar', 'Black, Elk', 'Berio, Luciano',
'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
];
🍺 filter();
1. Filter the list of inventors for those who were born in the 1500's
戻り条件
const answer1 = inventors.filter((inventor) => {
return (1500 <= inventor.year && inventor.year < 1600) ? true : false;
})
/*0: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
1: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}*/
🌴 map()
2. Give us an array of the inventors first and last names
const answer2 = inventors.map((inventor) => {
return inventor.first + ' ' + inventor.last
})
/*['Albert Einstein', 'Isaac Newton', 'Galileo Galilei', 'Marie Curie', 'Johannes Kepler', 'Nicolaus Copernicus', 'Max Planck', 'Katherine Blodgett', 'Ada Lovelace', 'Sarah E. Goode', 'Lise Meitner', 'Hanna Hammarström']*/
6. Boulevards
😓 間違った問題を理解しました...ううう
create a list of Boulevards in Paris that contain 'de' anywhere in the name
フランス・パリの道路名のような「~の道」Boulevards de~
一つだけやればいいと思った. const answer6 = inventors.map((inventor) => {
return `Boulevard de ${inventor.first} ${inventor.last}`
})
/*
['Boulevard de Ada Lovelace', 'Boulevard de Sarah E. Goode', 'Boulevard de Johannes Kepler', 'Boulevard de Marie Curie', 'Boulevard de Nicolaus Copernicus', 'Boulevard de Albert Einstein', 'Boulevard de Galileo Galilei', 'Boulevard de Hanna Hammarström', 'Boulevard de Katherine Blodgett', 'Boulevard de Isaac Newton', 'Boulevard de Max Planck', 'Boulevard de Lise Meitner']*/
🤩 再オープン
実際にはurl https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
にdeが含まれている道路だけが戻る必要があります.const answer6 = inventors.map((inventor) => {
return `Boulevard de ${inventor.first} ${inventor.last}`
})
console.log(answer6)
const category = document.querySeletor('.mw-category');
// const links = category.querySelectorAll('a');
//const de = links.map(link => link.textContent);
//이렇게 하면 작동하지 않는다. querySelector로 선택해 NodeList가 반환되므로 배열로 바꾸어줘야 한다.
const links = Array.from(category.querySelectorAll('a'));//전개연산자도 사용가능
const de = links
.map(link => link.textContent)
.filter(streetName => streetName.includes('de'));
🥨 sort()
3. Sort the inventors by birthdate, oldest to youngest
const answer3 = inventors.sort((a, b) => {
return a.year - b.year;
}
)
/*0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}*/
5. Sort the inventors by years lived
ソートする条件の取得
const answer5 = inventors.sort((a, b) => {
return (a.passed - a.year) - (b.passed - b.year);
})
/*
0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}*/
7. sort Exercise
String.prototype.localeCompare()
ThelocaleCompare()メソッド
比較文字列がソート前、後、または同じ順序にあるかを示す数値を返します.Sort the people alphabetically by last name
const answer7 = inventors.sort((a, b) => {
return a.last.localeCompare(b.last)
})
/*
0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}
*/
🍟 reduce()
4. How many years did all the inventors live all together?
プール1:計算機の初期値の指定
これによりcurrのデータ型がnumberであっても,アキュムレータの総和を正確に求めることはできない.オリジナルが対象だからかもしれません. const answer4 = inventors.reduce((sum, curr) => {
const currYear = (curr.passed - curr.year)
return sum + currYear;
})
[object Object]78
index-START.html:78 [object Object]7859
index-START.html:78 [object Object]785984
index-START.html:78 [object Object]78598437
index-START.html:78 [object Object]7859843780
index-START.html:78 [object Object]785984378050
index-START.html:78 [object Object]78598437805089
index-START.html:78 [object Object]7859843780508967
index-START.html:78 [object Object]785984378050896790
index-START.html:78 [object Object]78598437805089679076
index-START.html:81 [object Object]7859843780508967907681
初期値が0の場合、解決されます.
ゼロから始め、「数字」を加えることができます. const answer4 = inventors.reduce((sum, curr) => {
return sum+ (curr.passed - curr.year)
},0)
//861
プール2:まずarrayを作成する
他の解釈でも解釈してみます.
元のオブジェクトから必要な数値を返す-生存期間を返すだけで配列を作成し、配列でreduceで合計します. const yearArr = inventors.map((inventor) => {
return inventor.passed - inventor.year
})
//[70, 78, 59, 84, 37, 80, 50, 89, 67, 90, 76, 81]
const answer4 = yearArr.reduce((sum, curr) => {
return sum + curr;
})
//861
8. Reduce Exercise
// Sum up the instances of each of these
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
😓 間違った問題を理解しました...ううう
ハハ...これも問題を間違えた.重複値を排除して簡潔にするのではなく、まず私の答えは以下の通りです.
Reduceを使用してアレイ内の重複値を除去
const answer8 = data.reduce((sum, curr) => {
return sum.includes(curr) ? sum : [...sum,curr];
}, [])
//['car', 'truck', 'bike', 'walk', 'van']
const answer1 = inventors.filter((inventor) => {
return (1500 <= inventor.year && inventor.year < 1600) ? true : false;
})
/*0: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
1: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}*/
2. Give us an array of the inventors first and last names
const answer2 = inventors.map((inventor) => {
return inventor.first + ' ' + inventor.last
})
/*['Albert Einstein', 'Isaac Newton', 'Galileo Galilei', 'Marie Curie', 'Johannes Kepler', 'Nicolaus Copernicus', 'Max Planck', 'Katherine Blodgett', 'Ada Lovelace', 'Sarah E. Goode', 'Lise Meitner', 'Hanna Hammarström']*/
6. Boulevards
😓 間違った問題を理解しました...ううう
create a list of Boulevards in Paris that contain 'de' anywhere in the name
フランス・パリの道路名のような「~の道」Boulevards de~一つだけやればいいと思った.
const answer6 = inventors.map((inventor) => {
return `Boulevard de ${inventor.first} ${inventor.last}`
})
/*
['Boulevard de Ada Lovelace', 'Boulevard de Sarah E. Goode', 'Boulevard de Johannes Kepler', 'Boulevard de Marie Curie', 'Boulevard de Nicolaus Copernicus', 'Boulevard de Albert Einstein', 'Boulevard de Galileo Galilei', 'Boulevard de Hanna Hammarström', 'Boulevard de Katherine Blodgett', 'Boulevard de Isaac Newton', 'Boulevard de Max Planck', 'Boulevard de Lise Meitner']*/
🤩 再オープン
実際にはurl
https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
にdeが含まれている道路だけが戻る必要があります.const answer6 = inventors.map((inventor) => {
return `Boulevard de ${inventor.first} ${inventor.last}`
})
console.log(answer6)
const category = document.querySeletor('.mw-category');
// const links = category.querySelectorAll('a');
//const de = links.map(link => link.textContent);
//이렇게 하면 작동하지 않는다. querySelector로 선택해 NodeList가 반환되므로 배열로 바꾸어줘야 한다.
const links = Array.from(category.querySelectorAll('a'));//전개연산자도 사용가능
const de = links
.map(link => link.textContent)
.filter(streetName => streetName.includes('de'));
🥨 sort()
3. Sort the inventors by birthdate, oldest to youngest
const answer3 = inventors.sort((a, b) => {
return a.year - b.year;
}
)
/*0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}*/
5. Sort the inventors by years lived
ソートする条件の取得
const answer5 = inventors.sort((a, b) => {
return (a.passed - a.year) - (b.passed - b.year);
})
/*
0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}*/
7. sort Exercise
String.prototype.localeCompare()
ThelocaleCompare()メソッド
比較文字列がソート前、後、または同じ順序にあるかを示す数値を返します.Sort the people alphabetically by last name
const answer7 = inventors.sort((a, b) => {
return a.last.localeCompare(b.last)
})
/*
0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}
*/
🍟 reduce()
4. How many years did all the inventors live all together?
プール1:計算機の初期値の指定
これによりcurrのデータ型がnumberであっても,アキュムレータの総和を正確に求めることはできない.オリジナルが対象だからかもしれません. const answer4 = inventors.reduce((sum, curr) => {
const currYear = (curr.passed - curr.year)
return sum + currYear;
})
[object Object]78
index-START.html:78 [object Object]7859
index-START.html:78 [object Object]785984
index-START.html:78 [object Object]78598437
index-START.html:78 [object Object]7859843780
index-START.html:78 [object Object]785984378050
index-START.html:78 [object Object]78598437805089
index-START.html:78 [object Object]7859843780508967
index-START.html:78 [object Object]785984378050896790
index-START.html:78 [object Object]78598437805089679076
index-START.html:81 [object Object]7859843780508967907681
初期値が0の場合、解決されます.
ゼロから始め、「数字」を加えることができます. const answer4 = inventors.reduce((sum, curr) => {
return sum+ (curr.passed - curr.year)
},0)
//861
プール2:まずarrayを作成する
他の解釈でも解釈してみます.
元のオブジェクトから必要な数値を返す-生存期間を返すだけで配列を作成し、配列でreduceで合計します. const yearArr = inventors.map((inventor) => {
return inventor.passed - inventor.year
})
//[70, 78, 59, 84, 37, 80, 50, 89, 67, 90, 76, 81]
const answer4 = yearArr.reduce((sum, curr) => {
return sum + curr;
})
//861
8. Reduce Exercise
// Sum up the instances of each of these
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
😓 間違った問題を理解しました...ううう
ハハ...これも問題を間違えた.重複値を排除して簡潔にするのではなく、まず私の答えは以下の通りです.
Reduceを使用してアレイ内の重複値を除去
const answer8 = data.reduce((sum, curr) => {
return sum.includes(curr) ? sum : [...sum,curr];
}, [])
//['car', 'truck', 'bike', 'walk', 'van']
const answer3 = inventors.sort((a, b) => {
return a.year - b.year;
}
)
/*0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}*/
const answer5 = inventors.sort((a, b) => {
return (a.passed - a.year) - (b.passed - b.year);
})
/*
0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}*/
const answer7 = inventors.sort((a, b) => {
return a.last.localeCompare(b.last)
})
/*
0: {first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979}
1: {first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543}
2: {first: 'Marie', last: 'Curie', year: 1867, passed: 1934}
3: {first: 'Albert', last: 'Einstein', year: 1879, passed: 1955}
4: {first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642}
5: {first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905}
6: {first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909}
7: {first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630}
8: {first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852}
9: {first: 'Lise', last: 'Meitner', year: 1878, passed: 1968}
10: {first: 'Isaac', last: 'Newton', year: 1643, passed: 1727}
11: {first: 'Max', last: 'Planck', year: 1858, passed: 1947}
*/
4. How many years did all the inventors live all together?
プール1:計算機の初期値の指定
これによりcurrのデータ型がnumberであっても,アキュムレータの総和を正確に求めることはできない.オリジナルが対象だからかもしれません.
const answer4 = inventors.reduce((sum, curr) => {
const currYear = (curr.passed - curr.year)
return sum + currYear;
})
[object Object]78
index-START.html:78 [object Object]7859
index-START.html:78 [object Object]785984
index-START.html:78 [object Object]78598437
index-START.html:78 [object Object]7859843780
index-START.html:78 [object Object]785984378050
index-START.html:78 [object Object]78598437805089
index-START.html:78 [object Object]7859843780508967
index-START.html:78 [object Object]785984378050896790
index-START.html:78 [object Object]78598437805089679076
index-START.html:81 [object Object]7859843780508967907681
初期値が0の場合、解決されます.ゼロから始め、「数字」を加えることができます.
const answer4 = inventors.reduce((sum, curr) => {
return sum+ (curr.passed - curr.year)
},0)
//861
プール2:まずarrayを作成する
他の解釈でも解釈してみます.
元のオブジェクトから必要な数値を返す-生存期間を返すだけで配列を作成し、配列でreduceで合計します.
const yearArr = inventors.map((inventor) => {
return inventor.passed - inventor.year
})
//[70, 78, 59, 84, 37, 80, 50, 89, 67, 90, 76, 81]
const answer4 = yearArr.reduce((sum, curr) => {
return sum + curr;
})
//861
8. Reduce Exercise
// Sum up the instances of each of these
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
😓 間違った問題を理解しました...ううう
ハハ...これも問題を間違えた.重複値を排除して簡潔にするのではなく、まず私の答えは以下の通りです.
Reduceを使用してアレイ内の重複値を除去
const answer8 = data.reduce((sum, curr) => {
return sum.includes(curr) ? sum : [...sum,curr];
}, [])
//['car', 'truck', 'bike', 'walk', 'van']
🤩 再オープン
const transportation = data.reduce(function(obj, item) {
if(!obj[item]) {
obj[item] = 0;
}
obj[item]++;
return obj;
}, {});
/*
bike: 2
car: 5
truck:3
van: 2
walk: 2
*/
Reference
reduce-アレイ値重複除外
How to Remove Array Duplicates in ES6
Dongmin-Jang Array javascript
Reference
この問題について([JS30] -4) Array Cardio Day 1), 我々は、より多くの情報をここで見つけました https://velog.io/@gygy/JS30-4-Array-Cardio-Day1テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol