30天JS挑战(第七天)

59 阅读4分钟

第七天挑战(数据处理)

地址:javascript30.com/

所有内容均上传至gitee,答案不唯一,仅代表本人思路

中文详解:github.com/soyaine/Jav…

该详解是Soyaine及其团队整理编撰的,是对源代码的详解强烈推荐大家观看学习!!!!!!!

本人gitee:gitee.com/thats-all-r…

题目及数据

题目:

  • 是否有人超过 19 岁?
  • 是否所有人都是成年人?
  • 找到 ID 号为 823423 的评论
  • 删除 ID 号为 823423 的评论
  • 获取此 ID 对应元素在数组中所处的位置
  • 利用位置,删除该子元素
  • 利用数组拼接方法删除该元素

数据:

/*1-2题*/
const people = [
  { name: 'Wes', year: 1988 },
  { name: 'Kait', year: 1986 },
  { name: 'Irv', year: 1970 },
  { name: 'Lux', year: 2015 }
];

/*第3-7题*/
const comments = [
  { text: 'Love this!', id: 523423 },
  { text: 'Super good', id: 823423 },
  { text: 'You are the best', id: 2039842 },
  { text: 'Ramen is my fav food ever', id: 123523 },
  { text: 'Nice Nice Nice!', id: 542328 }
];

本人代码及思路分析

仅提供布局及逻辑代码

逻辑: 是否有人超过 19 岁?

// 1.是否有人超过 19 岁?
console.log( people.some(item => {   
  return new Date().getFullYear() - item.year > 19
}))

分析:

  • some:测试数组中是否至少有一个元素通过了由提供的函数实现的测试。如果在数组中找到一个元素使得提供的函数返回 true,则返回 true;否则返回 false。它不会修改数组。

逻辑:是否所有人都是成年人?

//2.是否所有人都是成年人?
console.log(people.every(item => new Date().getFullYear() - item.year >= 18))

分析:

  • every: 测试一个数组内的所有元素是否都能通过指定函数的测试。它返回一个布尔值。

逻辑:找到 ID 号为 823423 的评论

//3.找到 ID 号为 823423 的评论
console.log(comments.find(item => {
  return item.id === 823423
}).text)

分析:

  • find: 返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined

逻辑:删除 ID 号为 823423 的评论

//4.删除 ID 号为 823423 的评论
comments.splice(comments.findIndex(item => item.id === 823423),1)
console.log(comments)

分析:

  • splice: 通过移除或者替换已存在的元素和/或添加新元素就地改变一个数组的内容。

逻辑:获取此 ID 对应元素在数组中所处的位置

//5.获取此 ID 对应元素在数组中所处的位置
console.log(comments.findIndex(item => item.id === 823423))

分析:

  • findindex: 返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。

逻辑:利用位置,删除该子元素

//6.利用位置,删除该子元素
comments.splice(comments.findIndex(item => item.id === 823423),1)
console.log(comments)

分析:

  • splice: 通过移除或者替换已存在的元素和/或添加新元素就地改变一个数组的内容。
  • findindex: 返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。

逻辑:利用数组拼接方法删除该数组

//7.利用数组拼接方法删除该数组
const index = comments.findIndex(item => item.id === 823423)
const newComments = [
  ...comments.slice(0, index),
  ...comments.slice(index + 1)
];

分析:

  • slice: 方法返回一个新的数组对象,这一对象是一个由 startend 决定的原数组的浅拷贝(包括 start,不包括 end),其中 startend 代表了数组元素的索引。原始数组不会被改变。
  • ... :结构赋值可以将数组中的值或对象的属性取出,赋值给其他变量。

官方代码

官方代码仅代表该案例原作者思路,不唯一

这里官方文档题目是英文,我就不在这里翻译了,大家对照我上面的题目看就行,顺序和题目都是一致的

建议直接去看Soyaine的中文详解 建议直接去看Soyaine的中文详解 建议直接去看Soyaine的中文详解

逻辑

<script>
// ## Array Cardio Day 2const people = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 }
];
​
const comments = [
{ text: 'Love this!', id: 523423 },
{ text: 'Super good', id: 823423 },
{ text: 'You are the best', id: 2039842 },
{ text: 'Ramen is my fav food ever', id: 123523 },
{ text: 'Nice Nice Nice!', id: 542328 }
];
​
// Some and Every Checks
// Array.prototype.some() // is at least one person 19?
// const isAdult = people.some(function(person) {
  //   const currentYear = (new Date()).getFullYear();
  //   if(currentYear - person.year >= 19) {
    //     return true;
    //   }
  // });const isAdult = people.some(person => ((new Date()).getFullYear()) - person.year >= 19);
​
console.log({isAdult});
// Array.prototype.every() // is everyone 19?const allAdults = people.every(person => ((new Date()).getFullYear()) - person.year >= 19);
console.log({allAdults});
​
// Array.prototype.find()
// Find is like filter, but instead returns just the one you are looking for
// find the comment with the ID of 823423
​
​
const comment = comments.find(comment => comment.id === 823423);
​
console.log(comment);
​
// Array.prototype.findIndex()
// Find the comment with this ID
// delete the comment with the ID of 823423
const index = comments.findIndex(comment => comment.id === 823423);
console.log(index);
​
// comments.splice(index, 1);const newComments = [
...comments.slice(0, index),
...comments.slice(index + 1)
];

建议直接去看Soyaine的中文详解 建议直接去看Soyaine的中文详解 建议直接去看Soyaine的中文详解