这是我的密码,所以没有文档类型或样板。我需要帮助让这些按钮开始工作。单击OMG按钮时,它将显示->This数组中的信息:${item.type} \\{item.address}\ ${item.rent}当单击按钮海滩家园时,它将显示-> ${item.type} \{item.address}\\{item.rent}}。根据js中提供的信息和Js中的评论,我将如何处理这一问题。
<!--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脚本
//
// 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);
发布于 2022-08-29 04:16:36
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)}
<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
元素,并为每个家庭都设置一个过滤器,让数据决定在代码中不对其进行硬编码。
如果你想让我给你看的话,这将是一个很好的练习,但你可以自己试试。
https://stackoverflow.com/questions/73523923
复制相似问题