簡體   English   中英

如何使用 ForEach 和 Filter 對數組進行排序並將其顯示到控制台?

[英]How to sort the array and display it to the console using ForEach and Filter?

如何使用 ForEach 和 Filter 對數組進行排序並顯示到控制台,首先需要顯示所有姓氏,然后顯示帶有字母“F”的姓氏? 我究竟做錯了什么? '使用嚴格'; // https://reqres.in/api/users

fetch('https://reqres.in/api/users?per_page=12')
    .then((response) => {
       return  response.json();
    })
    .then((body) => {
        body?.data.forEach((item) => {
            console.log(item.last_name);
        })
        const filtered = body?.data.filter((item) => {
            return item.last_name === 'F';
            console.log(filtered);
        });
    console.log(filtered);
 });

您沒有檢查姓氏的第一個字母

 fetch('https://reqres.in/api/users?per_page=12').then((response) => { return response.json(); }).then((body) => { const filtered = body?.data.filter((item) => { return item.last_name[0] === 'F'; }); console.log(filtered); });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM