当前位置: 首页 > 工具软件 > Cardio > 使用案例 >

04 - Array Cardio Day 1

云利
2023-12-01

04 - Array Cardio Day 1

// Array.prototype.filter()
    // 1. Filter the list of inventors for those who were born in the 1500's
    const filterArr = inventors.filter(inventors => inventors.year>=1500 && inventors.year <1600);
    Console.log(filterArr);
   const Arr = inventors.map(inventors => `${inventors.first} ${inventors.passed}`);
    Console.log(Arr);
const oldersArr = inventors.sort((a,b)=> a.year - b.year);
 console.table(oldersArr); 
  // Array.prototype.reduce()
  // 4. How many years did all the inventors live?
  // const totalYears = inventors.reduce((total, inventor) => {
  //   return total + (inventor.passed - inventor.year);
  // }, 0);
  const totalYears = inventors.reduce((total,inventor)=>{
    return total + (inventor.last - inventor.year);
  },0);

  console.log(totalYears);
const liveYear = inventors.sort((a,b)=> (a.passed - a.year) - (b.passed - b.year) );
console.log(liveYear);
    // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
    // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris

const category = document.querySelector('.mw-category');
const links = Array.from(category.querySelectorAll('a'));
const de = links
            .map(link => link.textContent)
           .filter(streetName => streetName.includes('de'));
 const nameArr = people.sort((a,b) =>{
    const [afirst,alast] = a.split(',');
    const [bfirst,blast] = b.split(',');
    return alast > blast ? 1 : -1;
  });
  console.log(nameArr);

注意:字符串排序 ,不可直接用-,会出错

 const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck', 'pogostick'];

  const transportation = data.reduce(function(obj, item) {
    if (!obj[item]) {
      obj[item] = 0;
    }
    obj[item]++;
    return obj;
  }, {});

  console.log(transportation);
 类似资料:

相关阅读

相关文章

相关问答