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>
demo = document.getElementById("demo");

var peopleList = [
  {
  name: "Joe",
  age: 3
  }, 
  {
  name: "Sara",
  age: 10
  }, 
  {
  name: "Luis",
  age: 18
  }, 
  {
  name: "Franzi",
  age: 20
  }
];

const adultsList = peopleList.filter((people)=>{
  return people.age >= 10;
})

function printAdultList(person, index) {
  demo.innerHTML +=  "index[" + index + "]: " + person.name + " " + person.age + "<br>"; 
}

function returnAdults()  {
	return adultsList.forEach(printAdultList);
}
</script>

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