๋ฆฌ์คํธ
let ar = [
{
n:'ํธ๋์ด',
a: 10,
},
{
n:'์ฝ๋ผ๋ฆฌ',
a: 20,
},
{
n:'๋
์๋ฆฌ',
a: 30,
}
]
console.log(1);
console.log(ar);
console.log(2);
for(const v of ar){
console.log(v);
}
// { n: 'ํธ๋์ด', a: 10 }
// { n: '์ฝ๋ผ๋ฆฌ', a: 20 }
// { n: '๋
์๋ฆฌ', a: 30 }
console.log(3);
console.log(ar);
console.log(4);
typeof
let ar = [80, 20, 10, 15];
console.log(ar, typeof(ar));
let br = ar.toString();
console.log(br, typeof(br));
let st01 = '80,20,10,15';
console.log(st01, typeof(st01));
// [ 80, 20, 10, 15 ] object
// 80,20,10,15 string
// 80,20,10,15 string
- ๋ณ์ ํ์ ํ์ธ
push & pop
let ar = [80, 20, 10, 15]
// pop()* // ์๋ณธ ๋ฐ์ดํฐ๊ฐ ์์๋๋ ํจ์๋ผ๋ ์๋ฏธ
let num = ar.pop();
console.log(ar, num); // [80, 20, 10] 15
// push()*
num = ar.push(20);
console.log(ar, num); // [80, 20, 10, 20] 4 // size
- ์๋ณธ ๋ฐ์ดํฐ๊ฐ ์์์ด ๋๋์ง ์๋๋์ง์ ์ฌ๋ถ๊ฐ ์ค์ํ๋ค.
- push : ๋ฐฐ์ด์ ๋งจ ๋ค์ ๊ฐ ์ถ๊ฐ
- pop : ๋ฐฐ์ด์ ๋งจ ๋์ ๊ฐ ์ ๊ฑฐ
shift & unshift
// shift
let ar = [10, 20, 30]
// shift()* // ์๋ณธ ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋๋ค.
console.log(ar.shift()); // 10
console.log(ar); // [20, 30]
console.log(ar.unshift(40)); // 3
console.log(ar); // [40, 20, 30]
console.log(ar.unshift(50, 60 ,70)); // 6
console.log(ar); // [50, 60, 70, 40, 20, 30]
- shift : ๋ฐฐ์ด์ ๋งจ ์์ ๊ฐ ์ถ๊ฐ
- unshift : ๋ฐฐ์ด์ ๋งจ ์์ ๊ฐ ์ ๊ฑฐ
concat
// concat
let ar = [80, 20, 10, 15]
let br = ar.concat(40);
console.log(ar); // [80, 20, 10, 15]
console.log(br); // [80, 20, 10, 15, 40]
let cr = [40, 50, 60];
let dr = ar.concat(cr);
console.log(dr); // [80, 20, 10, 15, 40, 50, 60]
- ๋ฐฐ์ด์ ์๋ก ๋ค์ด์ค๋ ๊ฐ์ ์ฐ๊ฒฐ์์ผ์ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํ
join
// join
let ar = ['tiger', 'lion', 'cat'];
console.log(ar.join());
console.log(ar.join(''));
console.log(ar.join(' + '));
// tiger,lion,cat
// tigerlioncat
// tiger + lion + cat
- ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ค์ ์ฐ๊ฒฐ
sort
// sort
let ar = [52, 273, 103, 32];
function func(a, b) {
// return (a > b) ? +1 : -1;
return a - b;
}
ar.sort(func);
console.log(ar); // [ 32, 52, 103, 273 ]
let ar = [
{n:30, a:'์ผ์ฑ'},
{n:20, a:'๋กฏ๋ฐ'},
{n:10, a:'ํ๋'},
];
function func1(a, b) {
return a.n - b.n;
}
function func2(a, b) {
let c = a / 10 + a % 10;
let d = b / 10 + b % 10;
return c - d;
}
ar.sort(func1);
console.log(ar); // [ { n: 10, a: 'ํ๋' }, { n: 20, a: '๋กฏ๋ฐ' }, { n: 30, a: '์ผ์ฑ' } ]
ar.sort(func2);
console.log(ar); // [ { n: 30, a: '์ผ์ฑ' }, { n: 20, a: '๋กฏ๋ฐ' }, { n: 10, a: 'ํ๋' } ]
- sort : ๋ฐฐ์ด ์ ๋ ฌ
slice
// slice * // ์๋ณธ ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋๋ค.
let ar = [10, 20, 30, 40, 50, 60];
// s <= x < e
let br = ar.slice(2, 4);
console.log(br); // [30, 40]
- slice : ๋ฐฐ์ด ์๋ฅด๊ธฐ
- dictionary ๋๋ ๋ฐฐ์ด์ ๊ฒฝ์ฐ ์๋ณธ ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋๋ค. (์์ ๋ณต์ฌ)
splice
// splice
let ar = [1,2,3];
// [์์ ์์น, ์ญ์ ํญ๋ชฉ ์, ์ถ๊ฐ ํญ๋ชฉ ...]
ar.splice(1, 0, 10, 20, 30);
console.log(ar); // [ 1, 10, 20, 30, 2, 3 ]
ar.splice(1, 2, 88, 99);
console.log(ar); // [ 1, 88, 99, 30, 2, 3 ]
let br = [66, 55, 44, 33];
ar.splice(0, 0, br);
console.log(ar); // [ [ 66, 55, 44, 33 ], 1, 88, 99, 30, 2, 3 ]
- splice : ์ญ์ ํ ์์ ๊ฐ์ ๋ฐ ์ถ๊ฐํ ์์๋ฅผ ๋ฐ์์ ์๋ณธ ๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ์ง์ ์์ ๋ฐ ์ฐ๊ฒฐ
indexOf
// indexOf
let ar = ['tiger', 'cat', 'tiger', 'lion', 'apple'];
console.log(ar.indexOf('lion')); // 3
console.log(ar.indexOf('dog')); // -1
console.log(ar.indexOf('lion', 3)); // 3
console.log(ar.indexOf('cat', 10)); // -1
console.log(ar.indexOf('tiger')); // 0
console.log(ar.lastIndexOf('tiger')); // 2
- indexOf : ๋ฐฐ์ด ์์ ์ฐพ๊ธฐ(๊ฒ์)
every
// every
let ar = [1, 2, 10, 39, 20]
let br = [1, 2, 10, 50, 20]
function func(value) {
return value < 40;
}
console.log(ar.every(func)); // true
console.log(br.every( v => v < 40)); // false
// ๊ฐ์ํ ํ๊ธฐ ์
// console.log(br.every(
// (v) => { return v < 40; }
// ));
- every : ๋ฐฐ์ด์ ๋ชจ๋ ์์๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง(true / false) ๊ฒ์ฌ
some
// some
let ar = [1, 5, 11, 39, 21];
console.log(ar.some(v => v % 2 ==0)); // false
- some : ๋ฐฐ์ด์ ์์ ์ค ํ๋๋ง ์กฐ๊ฑด์ ํต๊ณผํ๋ฉด true
filter
// filter
let ar = [1, 6, 11, 39, 21];
let br = ar.filter( v => v < 15 );
console.log(br); // [ 1, 6, 11 ]
- filter : ์กฐ๊ฑด์ ์ค์ ํ๊ณ ๊ทธ ์กฐ๊ฑด์ ๋ง๋ ๋ฐฐ์ด ์์๋ง์ผ๋ก ์๋ก์ด ๋ฐฐ์ด ์์ฑํ์ฌ ๋ฐํ
reduce
// reduce
// ํํ : array.reduce((๋์ ๊ฐ, ํ์ฟ๊ฐ, ์ธ๋ฑ์ค, ์์) => { return ๊ฒฐ๊ณผ }, ์ด๊น๊ฐ);
// sum (๋ง์
)
var initialValue = 0;
var sum = [{x: 1}, {x: 2}, {x: 3}].reduce(
(accumulator, currentValue) => accumulator + currentValue.x, initialValue
);
console.log(sum); // 6
// concat
var flattened = [[0,1],[2,3],[4,5]].reduce(
(accumulator, currentValue) => accumulator.concat(currentValue),
[]
);
console.log(flattened); // [ 0, 1, 2, 3, 4, 5 ]
// ๊ฐ์ฒด ๋ด ์ธ์คํด์ค ๊ฐ์ ๊ฐ์ ์ธ๊ธฐ
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
var countedNames = names.reduce(function (allNames, name){
if(name in allNames){
allNames[name]++;
}
else{
allNames[name] = 1;
}
console.log(allNames);
return allNames;
}, {});
console.log(countedNames); // { Alice: 2, Bob: 1, Tiff: 1, Bruce: 1 }
- ์ด๊ธฐ๊ฐ์ ์ ์ด์ฃผ์ง ์์ผ๋ฉด ์๋์ผ๋ก 0๋ฒ์งธ ์ธ๋ฑ์ค ๊ฐ์ด ์ด๊ธฐ๊ฐ์ผ๋ก ์ค์
- sort, every, some, find ๋ฑ๋ reduce๋ก ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค.
์์ฑ์ผ๋ก ๊ฐ์ฒด ๋ถ๋ฅํ๊ธฐ
// ์์ฑ์ผ๋ก ๊ฐ์ฒด ๋ถ๋ฅํ๊ธฐ
var people = [
{ name: 'Alice', age: 21 },
{ name: 'Max', age: 20 },
{ name: 'Jane', age: 20 }
];
function groupBy(objectArray, property){
return objectArray.reduce(function (acc, obj){
var key = obj[property];
if(!acc[key]){
acc[key]=[];
}
console.log(acc[key]);
acc[key].push(obj);
return acc;
}, {});
}
var groupedPeople = groupBy(people, 'age');
console.log(groupedPeople);
// [] -> 21 ์ฒ์ ๋ค์ด์ด
// [] -> 20 ์ฒ์ ๋ค์ด์ด
// [ { name: 'Max', age: 20 } ] -> 20 ์ฐพ์
// {'20': [ { name: 'Max', age: 20 }, { name: 'Jane', age: 20 } ],
// '21': [ { name: 'Alice', age: 21 } ]}
- objectArray = list ๋ก, ์ฌ๊ธฐ์๋ people ๋ฆฌ์คํธ์ด๋ค.
- property = key ๋ก, ์ฌ๊ธฐ์๋ people ๋ฆฌ์คํธ์ age ์ด๋ค.
- acc ๋ ๋์ ๋ฆฌ์คํธ์ด๊ณ obj ๋ ํ์ฌ index์ ์์์ด๋ฏ๋ก key(= obj[property]) ๋ ๊ฐ index ์ age ๋ฅผ ์๋ฏธํ๋ค.
- ๋ฆฌ์คํธ๋ฅผ ์ธ๋ฑ์ค๋ณ๋ก ํ๋์ฉ ์ ๊ทผํ ๋ ๋์ ๋ฆฌ์คํธ์ ํ์ฌ ์ธ๋ฑ์ค์ age ๊ฐ๊ณผ ๊ฐ์ ๊ฐ์ด ์์ผ๋ฉด [] ๋ฅผ ๋ฐํํ๊ณ push ํ๋ค.
์คํ๋ ๋ ํ์ฅ ์ฐ์ฐ์์ ์ด๊ธฐ๊ฐ์ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ก ์ด๋ฃจ์ด์ง ๋ฐฐ์ด์ ๋ด๊ธด ๋ฐฐ์ด์ ์ฐ๊ฒฐํ๊ธฐ
// ์คํ๋ ๋ ํ์ฅ ์ฐ์ฐ์์ ์ด๊ธฐ๊ฐ์ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ก ์ด๋ฃจ์ด์ง ๋ฐฐ์ด์ ๋ด๊ธด ๋ฐฐ์ด์ ์ฐ๊ฒฐํ๊ธฐ
var friends = [{
name: 'Anna',
books: ['Bible', 'Harry Potter'],
age: 21
},{
name: 'Bob',
books: ['War and peace', 'Romeo and Juliet'],
age: 26
},{
name: 'Alice',
books: ['The Lord of the Rings', 'The Shining'],
age: 18
}];
var allbooks = friends.reduce(function(accumulator, currentValue){
return [...accumulator, ...currentValue.books];
}, ['Alphabet']);
console.log(allbooks);
//[
// 'Alphabet',
// 'Bible',
// 'Harry Potter',
// 'War and peace',
// 'Romeo and Juliet',
// 'The Lord of the Rings',
// 'The Shining'
//]
- ...currentValue.books : friends ๋ฆฌ์คํธ์์ ๊ฐ ์ธ๋ฑ์ค์ books ์์๋ฅผ ๊ฐ๋ณ ์์๋ก ๊ฐ์ ธ์์ ๋ค ๋ํ์ฌ allbooks ์ ๋ฐํํ๋ค.
* ์คํ๋ ๋ ์ฐ์ฐ์
// Spread ์ฐ์ฐ์
const ar = [1, 2, 3, 4, 5];
console.log(ar); // [ 1, 2, 3, 4, 5 ]
console.log(...ar); // 1, 2, 3, 4, 5
- ์คํ๋ ๋ ์ฐ์ฐ์๋ ๋ฐฐ์ด ์์๊ฐ ๊ฐ๋ณ์ ์ธ ์์๋ก ๋ฐํ๋๋ค.
๋ฐฐ์ด์ ์ค๋ณต ์ ๊ฑฐ
// ๋ฐฐ์ด์ ์ค๋ณต ์ ๊ฑฐ
let ar = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4];
let result = ar.sort().reduce((accumulator, current) => {
const length = accumulator.length;
console.log("accumulator: " + accumulator);
console.log("current: " + current);
console.log("length: " + length);
if (length === 0 || accumulator[length - 1] !== current) {
accumulator.push(current);
}
return accumulator;
}, []);
console.log(result); // [ 1, 2, 3, 4, 5 ]
- ๋์ ๋ฆฌ์คํธ ๊ธธ์ด๊ฐ 0 (=์ฒซ ์ ๋ ฅ) ์ด๊ฑฐ๋ ํ์ฌ index์ ์์๊ฐ๊ณผ ๊ทธ ์ ์์ ๊ฐ์ด ๋ค๋ฅด๋ฉด push ํ๊ณ , ๊ฐ์ผ๋ฉด ์ค๋ณต์ด๋ฏ๋ก push ํ์ง ์๋๋ค.
- sort() ํจ์๋ก ar ๋ฐฐ์ด์ด ์ด๋ฏธ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋ ์ํ์ด๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ๋ค.
// map ์์
let result = total.reduce((acc, cur) => {
acc.push(cur % 2 ? 'ํ์':'์ง์');
return acc;
}, []);
console.log(result); // [ 'ํ์', '์ง์', 'ํ์' ]
- reduce ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ด๊ธฐ๊ฐ์ ๋ฐฐ์ด๋ก ๋ง๋ค๊ณ push ํ๋ฉด map ๊ณผ ๊ฐ์์ง๋ค.
// filter ์์
let total = [1, 2, 3];
let result = total.reduce((acc, cur) => {
if(cur % 2 !== 0) { acc.push(cur); }
return acc;
}, []);
console.log(result); // [ 1, 3 ]
- reduce ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์กฐ๊ฑด๋ถ๋ก push ํ๋ฉด filter ์ ๊ฐ์์ง๋ค.
์์
๊ฐ์ฒด ๋ฐฐ์ด ์ค property ๊ฐ์ผ๋ก ๋์ด max, min ๊ตฌํ๊ธฐ
// age ์ต๋, ์ต์๊ฐ ๊ตฌํ๊ธฐ
var people = [
{ name: 'Alice', age: 25 },
{ name: 'Max', age: 23 },
{ name: 'Jane', age: 21 },
];
var max = people.reduce(
(max, current) => {
return current.age >= max.age ? current : max;
}, people[0]
)
var min = people.reduce(
(min, current) => {
return current.age < min.age ? current : min;
}, people[0]
)
console.log("max: ", max); // max: { name: 'Alice', age: 25 }
console.log("min: ", min); // min: { name: 'Jane', age: 21 }
- people์ age ๊ฐ ์ต๋ ์ต์ ๊ตฌํ๊ธฐ
- min, max ๊ตฌํ๋ ๊ณต์์ ์ด๋ ต์ง ์์๋ฐ ์ด๊ธฐ๊ฐ ์ค์ ์ด ์ค์ํ๋ค.
- ์ด๊ธฐ๊ฐ์ 0 ์ผ๋ก ์ก์์ค ๊ฒฝ์ฐ min ์ ๊ฒฝ์ฐ์ return 0 ์ด ๋์ด๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ ๊ฐ ๋น๊ต๋ฅผ ํ ๋ 0 ์ด ์๋ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค์ ๊ฐ๊ณผ ๋น๊ตํ๋๋ก ์ด๊ธฐ๊ฐ์ people[0] ์ผ๋ก ์ก์์ฃผ๊ณ people[0].age ๋ก ๊บผ๋ด ์ฌ์ฉํ์๋ค.
์ฐธ๊ณ : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Array.prototype.reduce() - JavaScript | MDN
reduce() ๋ฉ์๋๋ ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฃผ์ด์ง ๋ฆฌ๋์(reducer) ํจ์๋ฅผ ์คํํ๊ณ , ํ๋์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํฉ๋๋ค.
developer.mozilla.org
"์ด ํฌ์คํ ์ ์ฟ ํก ํํธ๋์ค ํ๋์ ์ผํ์ผ๋ก, ์ด์ ๋ฐ๋ฅธ ์ผ์ ์ก์ ์์๋ฃ๋ฅผ ์ ๊ณต๋ฐ์ต๋๋ค."
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
addEventListener ์ด๋ฒคํธ (input & change) (0) | 2022.04.25 |
---|---|
[JavaScript] ์๋ฐ์คํฌ๋ฆฝํธ Prototype (0) | 2021.09.30 |
[JavaScript] ๋๋ฒ๊ฑฐ ์๋ฌ ํด๊ฒฐ (0) | 2021.09.30 |
[JavaScript] ์๋ฐ ์คํฌ๋ฆฝํธ ํจ์ (0) | 2021.09.30 |
[JavaScript] ์๋ฐ ์คํฌ๋ฆฝํธ ๊ธฐ์ด (0) | 2021.09.28 |
๋๊ธ