html 在Javascript和打印结果中的对象数组中过滤(使用箭头功能)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 在Javascript和打印结果中的对象数组中过滤(使用箭头功能)相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<body>
<p>Click the button to check get the value of the first element in the array that has a value of 18 or more.</p>
<button onclick="returnAdults()">Try it</button>
<p id="demo"></p>
<script>
// We initialize our variable demo as an object
let demo = document.getElementById("demo");

// We initialize our array of objects
const peopleList = [
  {
  name: "Joe",
  age: 3
  }, 
  {
  name: "Sara",
  age: 10
  }, 
  {
  name: "Luis",
  age: 18
  }, 
  {
  name: "Franzi",
  age: 20
  }
];
// people could be any value, in this case is passed as an array
const adultsList = peopleList.filter((people)=>{
  return people.age >= 10;
})
// This function appends in variable demo the values of the array of objects adultList
const printAdultList = (person, index) => demo.innerHTML +=  "index[" + index + "]: " + person.name + " " + person.age + "<br>";
// This function uses forEach on the adultList array and calls another function (printAdulrList) to format the result
const returnAdults = () => adultsList.forEach(printAdultList);
</script>

以上是关于html 在Javascript和打印结果中的对象数组中过滤(使用箭头功能)的主要内容,如果未能解决你的问题,请参考以下文章

javascript如何去除对象的某个属性

javascript如何去除对象的某个属性

JavaScript笔记--- JSON(对象的创建,访问对象属性等;eval函数;模拟将数据库中的信息打印在页面的表格中)

如何在html textarea中打印javascript while循环结果?

读汤姆大叔《JavaScript变量对象》笔记

JavaScript隐藏的坑一,隐式调用toString