如何检查对象属性是否为true/false以及输出是否基于true/false



我一直在试图弄清楚这一点-如果有人有一些指针,我会通知它。基本上,我得到了一个有3个属性的对象,我需要检查其中一个属性是true还是false,以及基于返回的true/false。到目前为止,制作了这个代码,但据我所知,它无法识别对象属性。

const library = [
{
title: "Bill Gates",
author: "The Road Ahead",
isRead: true
},
{
title: "Steve Jobs",
author: "Walter Isaacson",
isRead: true
},
{
title: "Mockingjay: The Final Book of The Hunger Games",
author: "Suzanne Collins",
isRead: false
}
];
const showStatus = (arg) => {
let book = arg;
for(let i = 0;i < book.length; i++){
if(book.isRead === true){
console.log(`Already read ${book.title} by ${book.author}.`)
} else {
console.log(`You still need to read ${book.title} by ${book.author}`)
}
}
};
showStatus(library);

你拿的不是真正的书,比如

const book = arg[i], // iterate arg

您可以迭代这些书,并将单个条目销毁到这些部分中,然后使用这些部分。

const library = [{
title: "Bill Gates",
author: "The Road Ahead",
isRead: true
},
{
title: "Steve Jobs",
author: "Walter Isaacson",
isRead: true
},
{
title: "Mockingjay: The Final Book of The Hunger Games",
author: "Suzanne Collins",
isRead: false
}
];
const showStatus = (books) => {
for (const { title, author, isRead } of books) {
if (isRead) {
console.log(`Already read ${title} by ${author}.`);
} else {
console.log(`You still need to read ${title} by ${author}`);
}
}
};
showStatus(library);

最新更新