JavaScript

JS | Array

2021. 12. 22. 14:38
๋ชฉ์ฐจ
  1. 1. Declaration
  2. 2. Index position
  3. 3. Looping over an array
  4. 4. Addition, Deletion, copy

Array

1. Declaration

const arr1 = new Array();
const arr2 = [1, 2];

2. Index position

const fruits = ['๐ŸŽ', '๐ŸŒ'];
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
console.log(fruits[fruits.length - 1]);            // array์˜ index๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘

3. Looping over an array

mission. print all fruits
a. for
for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}

b. for of
for (let fruit of fruits) {
    console.log(fruit);
}

c. forEach        => callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
fruits.forEach((fruit) => console.log(fruit);

4. Addition, Deletion, copy

// push: add an item to the end
fruits.push('๐Ÿ…','๐Ÿ‹');
console.log(fruits);            // ['๐ŸŽ', '๐ŸŒ', '๐Ÿ…', '๐Ÿ‹'

// pop: remove an item from the end
fruits.pop();
console.log(fruits);            //  ['๐ŸŽ', '๐ŸŒ', '๐Ÿ…']

fruits.pop();
fruits.pop();
console.log(fruits);            //  ['๐ŸŽ']

//unshift: add an item to the beginning
fruits.unshift('๐Ÿ', '๐Ÿˆ');
console.log(fruits);            // ['๐Ÿ', '๐Ÿˆ', '๐ŸŽ']

//shift: remove an item from the beginning
fruits.shift();
console.log(fruits);            // ['๐Ÿˆ', '๐ŸŽ']

note!! shift, unshift are slower than pop, push
์ด๋ฏธ data๊ฐ€ ์กด์žฌํ•˜๋Š” ์•ž์ชฝ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜๊ธฐ ์œ„ํ•ด array ์ „์ฒด๊ฐ€ ์ˆ˜์ •๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋А๋ ค์ง„๋‹ค.

Array Quiz

// Q1. make a string out of an array
{
  const fruits = ['apple', 'banana', 'orange'];
}

// Q2. make an array out of a string
{
  const fruits = '๐ŸŽ, ๐Ÿฅ, ๐ŸŒ, ๐Ÿ’';
}

// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
  const array = [1, 2, 3, 4, 5];
}

// Q4. make new array without the first two elements
{
  const array = [1, 2, 3, 4, 5];
}

class Student {
  constructor(name, age, enrolled, score) {
    this.name = name;
    this.age = age;
    this.enrolled = enrolled;
    this.score = score;
  }
}
const students = [
  new Student('A', 29, true, 45),
  new Student('B', 28, false, 80),
  new Student('C', 30, true, 90),
  new Student('D', 40, false, 66),
  new Student('E', 18, true, 88),
];

// Q5. find a student with the score 90
{
}

// Q6. make an array of enrolled students
{
}

// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
}

// Q8. check if there is a student with the score lower than 50
{
}

// Q9. compute students' average score
{
}

// Q10. make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
}

// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
}

Array Quiz my answer

Q1. 
{
  const fruits = ['apple', 'banana', 'orange'];
  const string = fruits.toString();
}
*********************************************************
Q2.
{
  const fruits = '๐ŸŽ, ๐Ÿฅ, ๐ŸŒ, ๐Ÿ’';
  const arr = [];

  for (i=0; i < fruits.length; i +=4 ) {
    let item = fruits.slice(i, i+2);
    arr.push(item);
  }
  console.log(arr);
}
*********************************************************
Q3.
{
  const array = [1, 2, 3, 4, 5];
  const reverseArray = [];

  for(let i = array.length; i > 0; i--){
      let item = array.slice(i-1, i);
    reverseArray.push(item.toString());
  }

  console.log(reverseArray);
}

*********************************************************
Q4.
{
const array = [1, 2, 3, 4, 5];

const arr = [];

for ( i=0; i < 3; i++) {
    let lastItem = array.pop();
  arr.push(lastItem);
}
arr.reverse();

*********************************************************
Q5.
const result = students.find( ({score}) => score === 90);

*********************************************************
Q6.

*********************************************************
Q7.
function getField(input, field) {
    let output = [];
  for ( let i = 0; i < input.length; i++) {
      output.push(input[i][field]);
  }
  return output;
}

const answer = getField(students, "score");
*********************************************************

Q8.
const result = students.filter( stu => stu.score < 50);
*********************************************************

Q9.
let sum = 0;
for ( let i=0; i < students.length; i++) {
  sum += students[i]['score'];
}
let avg = sum / students.length;
console.log(avg);
*********************************************************

Q10.
let arr = [];
let result = 0;
for ( let i=0; i < students.length; i++) {
  arr.push(students[i]['score']);
  result = arr.toString();
}
console.log(result);
*********************************************************

Q.Bounes
let arr = [];
let result = 0;
for ( let i=0; i < students.length; i++) {
  arr.push(students[i]['score']);
  arr.sort();
  result = arr.toString();
}
console.log(result);

Array Quiz regular answer

// Q1. make a string out of an array
{
  const fruits = ['apple', 'banana', 'orange'];
  const result = fruits.join(`,`);
}

// Q2. make an array out of a string
{
  const fruits = '๐ŸŽ, ๐Ÿฅ, ๐ŸŒ, ๐Ÿ’';
  const result = fruits.split(',');
}

// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
  const array = [1, 2, 3, 4, 5];
  const result = array.reverse();
}

// Q4. make new array without the first two elements
{
  const array = [1, 2, 3, 4, 5];
  const result = array.slice(2,5);
}

class Student {
  constructor(name, age, enrolled, score) {
    this.name = name;
    this.age = age;
    this.enrolled = enrolled;
    this.score = score;
  }
}
const students = [
  new Student('A', 29, true, 45),
  new Student('B', 28, false, 80),
  new Student('C', 30, true, 90),
  new Student('D', 40, false, 66),
  new Student('E', 18, true, 88),
];

// Q5. find a student with the score 90
{
    consst result = students.find((student) => student.score === 90;); 
}

// Q6. make an array of enrolled students
{
    const result = students.filter((student) => students.enrolled);
}

// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
    const result = students.map((student) => student.score);
}

// Q8. check if there is a student with the score lower than 50
{
    const result = students.some((student) => student.score < 50);

        const result = students.every((student) => student.score < 50);
}

// Q9. compute students' average score
{
    const result = students.reduce((prev, curr) => prev + curr.score, 0);
        console.log(result / students.length);
}

// Q10. make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
    const result = students.map((student) => student.score).join());
}

// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
    const result = students
        .map(student => student.score)
        .sort((a, b) => a - b)
        .join();
    }
}

Q4. ์—์„œ์ฒ˜๋Ÿผ splice๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ฒŒ ๋˜๋ฉด ๊ธฐ์กด ๋ฐฐ์—ด ๋˜ํ•œ splice๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“  ์ƒˆ ๋ฐฐ์—ด๊ณผ ๊ฐ™์•„์ง„๋‹ค.
map: array ์•ˆ์˜ ์š”์†Œ๋“ค์„ callback ํ•จ์ˆ˜์—์„œ return๋œ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด ํ•ด์ค€๋‹ค.
some: array์˜ ์š”์†Œ ์ค‘์—์„œ callback ํ•จ์ˆ˜๊ฐ€ return์ด true์ธ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ค€๋‹ค.
every: array์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ callback ํ•จ์ˆ˜์˜ return์ด true์ธ์ง€ ํ™•์ธํ•ด์ค€๋‹ค.
Q10. API ๋“ค์„ ์„ž์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Q10.. E.g make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
    const result = students
            .map((student) => student.score)
            .filter(score => score >= 50)
            .join());
}

'JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JS | Promise ( Producer, Consumer)  (0) 2021.12.28
JS | ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์˜ ์‹œ์ž‘ ์ฝœ๋ฐฑ  (0) 2021.12.27
JS | Script option, Operator, Class and Object  (0) 2021.12.21
JS | logical and data type  (0) 2021.12.20
JS | clock(digital) , add back to top button  (0) 2021.12.17
  1. 1. Declaration
  2. 2. Index position
  3. 3. Looping over an array
  4. 4. Addition, Deletion, copy
'JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • JS | Promise ( Producer, Consumer)
  • JS | ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์˜ ์‹œ์ž‘ ์ฝœ๋ฐฑ
  • JS | Script option, Operator, Class and Object
  • JS | logical and data type
3jun
3jun
3jun
3jun
3jun
์ „์ฒด
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (94)
    • ํ”„๋กœ์ ํŠธ (5)
    • Web (9)
    • JavaScript (19)
    • React (12)
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (31)
    • Git (3)
    • AWS (3)
    • TypeScript (3)
    • CS (2)
    • Error (6)
    • ํšŒ๊ณ  (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • this.props.history.push
  • Promise
  • react
  • JavaScript
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”ํ…Œ
  • ๋ฐฑ์ค€ js
  • ๋ฐฑ์ค€js
  • ๋ฐฑ์ค€ ์˜จ๋ผ์ธ์ €์ง€
  • state
  • msw
  • ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ๋ฐฑ์ค€ ์ฝ”ํ…Œ
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ js
  • airbnb style guide
  • msw ์—๋Ÿฌ
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค js
  • outer environment
  • ์œ ํšจ์„ฑ ๋กœ์ง
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

hELLO ยท Designed By ์ •์ƒ์šฐ.
3jun
JS | Array
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.