공부

피망좋아해·2021년 1월 1일
0

forEach() : 배열 요소마다 한번씩 주어진 함수를 실행
map() : 배열 내의 모든 요소에 대해 주어진 함수를 호출한 결과를 새로운 배열로 반환


const axios = require("axios");
const cheerio = require("cheerio");
const log = console.log;

const getHtml = async () => {
try {
return await axios.get("https://www.yna.co.kr/sports/all");
} catch (error) {
console.error(error);
}
};

getHtml()
.then(html => {
let ulList = [];
const $ = cheerio.load(html.data);
const $bodyList = $("div.headline-list ul").children("li.section02");

$bodyList.each(function(i, elem) {
  ulList[i] = {
      title: $(this).find('strong.news-tl a').text(),
      url: $(this).find('strong.news-tl a').attr('href'),
      image_url: $(this).find('p.poto a img').attr('src'),
      image_alt: $(this).find('p.poto a img').attr('alt'),
      summary: $(this).find('p.lead').text().slice(0, -11),
      date: $(this).find('span.p-time').text()
  };
});

const data = ulList.filter(n => n.title);
						//item => item.title
                        //ulList 에 있는 모든 아이템에 밑에 타이틀만 가져온다
return data;

})
.then(res => log(res));


  1. const names = ["nico", "bongKi", "hadomi"]

    function addHeart(item) {
    return item + "😀"
    }

    const hearts = name.map(addHeart);

    console.log(hearts)

  2. const names = ["nico", "bongKi", "hadomi"]

    const addHeart = names.map(function ABCD(names) {
    return names + "😀";
    }

    console.log(addHeart)

  3. arrow function
    const names = ["nico", "bongKi", "hadomi"]

    const addHeart = names.map(item => {
    return item + "😀"});

  4. implicit

    const addHeart = names.map(item => item + "😀")

profile
Do not sell our comment

0개의 댓글