首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当单击相应的按钮时,我需要显示这个内容:/ ${item.type} the ${item.address} {item.rent} ${item.rent}

当单击相应的按钮时,我需要显示这个内容:/ ${item.type} the ${item.address} {item.rent} ${item.rent}
EN

Stack Overflow用户
提问于 2022-08-29 03:42:44
回答 1查看 31关注 0票数 0

这是我的密码,所以没有文档类型或样板。我需要帮助让这些按钮开始工作。单击OMG按钮时,它将显示->This数组中的信息:${item.type} \\{item.address}\ ${item.rent}当单击按钮海滩家园时,它将显示-> ${item.type} \{item.address}\\{item.rent}}。根据js中提供的信息和Js中的评论,我将如何处理这一问题。

代码语言:javascript
运行
复制
<!--The HTML-->
<button id="omg-homes">OMG Homes</button>
<div id="omg-homes-results"></div>

<button id="beach-homes">Beach Homes</button>
<div id="beach-homes-results"></div>

<div id="all-homes"></div>

下面是Java脚本

代码语言:javascript
运行
复制
//
// const suggestion = ['ded','eded','ede','e']
// Lakefront Beach Cabins OMG! National parks

const homes = [
    {
        type: 'Lakefront',
        image: 'https://picsum.photo/200?random=1',
        address: '1 Sample St',
        city: 'Boston',
        rent: 200
    },
    {
        type: 'Beach',
        image: 'https://picsum.photo/200?random=1',
        address: '1 James St',
        city: 'Boston',
        rent: 200
    },
    {
        type: 'Beach',
        image: 'https://picsum.photo/200?random=1',

        address: '1 Anon St',
        city: 'Boston',
        rent: 200
    },
    {
        type: 'National parks',
        image: 'https://picsum.photo/200?random=1',

        address: '1 Sample St',
        city: 'Boston',
        rent: 200
    },
  {
        type: 'National parks',
        image: 'https://picsum.photo/200?random=1',

        address: '1 Sample St',
        city: 'Boston',
        rent: 200
    },
   {
        type: 'National parks',
        image: 'https://picsum.photo/200?random=1',
        address: '1 Sample St',
        city: 'Boston',
        rent: 200
    },
    {
        type: 'OMG',
        image: 'https://picsum.photo/200?random=1',

        address: '1 Jerry St',
        city: 'Boston',
        rent: 200
    },
    {
        type: 'OMG',
        image: 'https://picsum.photo/200?random=1',
        address: '1 Josh St',
        city: 'Boston',
        rent: 200
    }
];

// const home1 =  {
//     type: 'Beach',
//     image: 'https://picsum.photo/200?random=1',
//
//     address: '1 Anon St',
//     city: 'Boston',
//     rent: 200
// };

// console.log(  home1.type === 'Beach'  );

/**
 * Finds beach homes
 *
 * @param item
 * @returns {boolean}
 */
function getBeachHomes(item){
    if( item.type === 'Beach' ){
        return true;
    }

    return false;
}

function getOMGHome(item){
    if(item.type === 'OMG'){
        return true;
    }

    return false;

}
// Array.filter( fn )
const beachHomes = homes.filter( getBeachHomes  );
const omgHomes = homes.filter( getOMGHome );

// Array.forEach( fn )

//beachHomes.forEach( function(item){
     //console.log(`
           // ${item.type} | ${item.address} |  ${item.rent}
// `)
// } )

omgHomes.forEach(  (item) => {
//     console.log(`
//            ${item.type} | ${item.address} |  ${item.rent}
// `)
} )



document.getElementById("beach-homes").addEventListener("click", beachHomes);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-29 04:16:36

代码语言:javascript
运行
复制
const homes = [
  {
    type: 'Lakefront',
    image: 'https://picsum.photo/200?random=1',
    address: '1 Sample St',
    city: 'Boston',
    rent: 200
  },
  {
    type: 'Beach',
    image: 'https://picsum.photo/200?random=1',
    address: '1 James St',
    city: 'Boston',
    rent: 200
  },
  {
    type: 'Beach',
    image: 'https://picsum.photo/200?random=1',

    address: '1 Anon St',
    city: 'Boston',
    rent: 200
  },
  {
    type: 'National parks',
    image: 'https://picsum.photo/200?random=1',

    address: '1 Sample St',
    city: 'Boston',
    rent: 200
  },
  {
    type: 'National parks',
    image: 'https://picsum.photo/200?random=1',

    address: '1 Sample St',
    city: 'Boston',
    rent: 200
  },
  {
    type: 'National parks',
    image: 'https://picsum.photo/200?random=1',
    address: '1 Sample St',
    city: 'Boston',
    rent: 200
  },
  {
    type: 'OMG',
    image: 'https://picsum.photo/200?random=1',

    address: '1 Jerry St',
    city: 'Boston',
    rent: 200
  },
  {
    type: 'OMG',
    image: 'https://picsum.photo/200?random=1',
    address: '1 Josh St',
    city: 'Boston',
    rent: 200
  }
];

const beachHomes = homes.filter(home=>home.type === "Beach")
const omgHomes = homes.filter(home=>home.type === "OMG")

const logHomes = (arr) => {
  arr.forEach(home=>console.log({type:home.type,address:home.address,rent:home.rent}))
}

const logBeach = () => {logHomes(beachHomes)}
const logOmg = ()=> {logHomes(omgHomes)}
代码语言:javascript
运行
复制
<button id="omg-homes" onclick="logOmg()">OMG Homes</button>
<div id="omg-homes-results"></div>

<button id="beach-homes" onclick="logBeach()">Beach Homes</button>
<div id="beach-homes-results"></div>

<div id="all-homes"></div>

当然,有更多可重用的方法来完成它,例如,您可以编写一个脚本来为每个不同类型的家庭在一个option元素中追加一个select元素,并为每个家庭都设置一个过滤器,让数据决定在代码中不对其进行硬编码。

如果你想让我给你看的话,这将是一个很好的练习,但你可以自己试试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73523923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档