我需要你的幫助; 我有一個物件陣列(稱為 bookArr,如下),如下所示。
bookArr 是:
[
{part: 'Text', value: 'Geralt is a witcher, a stronger and more resilient individual than any human, who earns his living by killing those creatures that dismay even the most daring: demons, orcs, evil elves.' , id: 1},
{part: 'Author', value: 'Andrzej Sapkowski', id: 2},
{part: 'Image', value: 'book_imgrt5432tp.jpg', id: 3},
{part: 'Title', value: "The guardian of the innocents. The Witcher. Vol. 1", id: 4}
]
例如,有時作者不為人知;所以一旦我為每個部分制作了一個回圈,我想知道作者(總是考慮到部分)是否丟失,因為我在 if 條件下需要它。
因此我做了如下:
if (Object.entries(results)[x][1].part == "Author") {
document.getElementById("auth").innerHTML = Object.entries(results)[x][1].value;
} else if (Object.entries(results)[x][1].part == "") {
do this....
}
我嘗試使用Object.entries(results)[x][1].part == "",但它不起作用。
我該如何解決我的問題?
編輯
如果缺少 Author,則缺少 Author 對應的整行,因此不存在
{1: {…}, 2: {…}, 3: {…}, 4: {…}}
1: {part: 'Text', value: 'Geralt is a witcher, a stronger and more resilient individual than any human, who earns his living by killing those creatures that dismay even the most daring: demons, orcs, evil elves.' , id: 1},
2: {part: 'Author', value: 'Andrzej Sapkowski', id: 2},
3:{part: 'Image', value: 'book_imgrt5432tp.jpg', id: 3},
4: {part: 'Title', value: "The guardian of the innocents. The Witcher. Vol. 1", id: 4}
[[Prototype]]: Object
完整代碼:(正如我所說,作者并不總是存在于陣列中(因此最后一條指令源自它))
while (x < Object.entries(results).length) {
if (Object.entries(results)[x][1].part == "Title"){
document.getElementById("title").innerHTML = Object.entries(results)[x][1].value;
}
else if (Object.entries(results)[x][1].part == "Text"){
document.getElementById("txt1").innerHTML = Object.entries(results)[x][1].value;
}
else if (Object.entries(results)[x][1].part == "Image"){
document.getElementById("img").src = Object.entries(results)[x][1].value;
}
else if (Object.entries(results)[x][1].part == "Author"){
document.getElementById("auth").innerHTML = Object.entries(results)[x][1].value;;
}
else if(!Object.entries(results)[x][1].some(item => item.part == 'Author')) {
console.log('yes')
document.getElementById("auth").style.display="none";
}
x ;
}
uj5u.com熱心網友回復:
如果我假設該陣列僅代表一本書,并且您想知道value該元素是否為空白part: "Author"或者沒有這樣的元素,那么Object.entries這里沒有任何作用。相反,使用find來找出是否有一個元素part: "Author",如果有,檢查那個元素看它是否有一個非空白value:
const authorElement = bookArr.find(({part}) => part === "Author");
if (!authorElement) {
// There is no `part: "Author"` element at all
} else if (!authorElement.value) {
// There is a `part: "Author"` element, but `value` is missing or blank
} else {
// There's a `part: "Author"` element and it has a non-blank `value`
}
現場示例:
顯示代碼片段
function check(label, array) {
const authorElement = array.find(({part}) => part === "Author");
if (!authorElement) {
// There is no `part: "Author"` element at all
console.log(label, "no Author part");
} else if (!authorElement.value) {
// There is a `part: "Author"` element, but `value` is missing or blank
console.log(label, "Author part has missing or blank value");
} else {
// There's a `part: "Author"` element and it has a non-blank `value`
console.log(label, `Author part has value "${authorElement.value}"`);
}
}
const bookArr = [
{part: 'Text', value: 'Geralt is a witcher, a stronger and more resilient individual than any human, who earns his living by killing those creatures that dismay even the most daring: demons, orcs, evil elves.' , id: 1},
{part: 'Author', value: 'Andrzej Sapkowski', id: 2},
{part: 'Image', value: 'book_imgrt5432tp.jpg', id: 3},
{part: 'Title', value: "The guardian of the innocents. The Witcher. Vol. 1", id: 4}
];
check("bookArr (array from the question):", bookArr);
const arrayWithoutElement = [
{part: 'Text', value: 'Geralt is a witcher, a stronger and more resilient individual than any human, who earns his living by killing those creatures that dismay even the most daring: demons, orcs, evil elves.' , id: 1},
{part: 'Image', value: 'book_imgrt5432tp.jpg', id: 3},
{part: 'Title', value: "The guardian of the innocents. The Witcher. Vol. 1", id: 4}
];
check("arrayWithoutElement:", arrayWithoutElement);
const arrayWithMissingValue = [
{part: 'Text', value: 'Geralt is a witcher, a stronger and more resilient individual than any human, who earns his living by killing those creatures that dismay even the most daring: demons, orcs, evil elves.' , id: 1},
{part: 'Author', id: 2},
{part: 'Image', value: 'book_imgrt5432tp.jpg', id: 3},
{part: 'Title', value: "The guardian of the innocents. The Witcher. Vol. 1", id: 4}
];
check("arrayWithMissingValue:", arrayWithMissingValue);
const arrayWithBlankValue = [
{part: 'Text', value: 'Geralt is a witcher, a stronger and more resilient individual than any human, who earns his living by killing those creatures that dismay even the most daring: demons, orcs, evil elves.' , id: 1},
{part: 'Author', value: '', id: 2},
{part: 'Image', value: 'book_imgrt5432tp.jpg', id: 3},
{part: 'Title', value: "The guardian of the innocents. The Witcher. Vol. 1", id: 4}
];
check("arrayWithMissingValue:", arrayWithBlankValue);
uj5u.com熱心網友回復:
如果您只想檢查陣列是否包含part:Author,只需使用Array.find()并且您不需要運行回圈。
更新:沒有定義xand 使用Object.entries(results)[1]而不是Object.entries(results)[x][1]
//No author
let results = [
{part: 'Text', value: 'Geralt is a witcher, a stronger and more resilient individual than any human, who earns his living by killing those creatures that dismay even the most daring: demons, orcs, evil elves.' , id: 1},
{part: 'Image', value: 'book_imgrt5432tp.jpg', id: 3},
{part: 'Title', value: "The guardian of the innocents. The Witcher. Vol. 1", id: 4}
]
function check(){
let x = 0
if(!Object.entries(results)[1].find(item => item.part == 'Author')) {
console.log('no author')
document.getElementById("idframe").style.display="none";
}
while (x < Object.entries(results).length) {
if (Object.entries(results)[x][1].part == "Title"){
document.getElementById("title").innerHTML = Object.entries(results)[x][1].value;
}
else if (Object.entries(results)[x][1].part == "Text"){
document.getElementById("txt1").innerHTML = Object.entries(results)[x][1].value;
}
else if (Object.entries(results)[x][1].part == "Image"){
document.getElementById("img").src = Object.entries(results)[x][1].value;
}
else if (Object.entries(results)[x][1].part == "Author"){
document.getElementById("auth").innerHTML = Object.entries(results)[x][1].value;
console.log(' have author')
}
x ;
}
}
check()
//Without author
<div id=img></div>
<div id=title></div>
<div id=txt1></div>
<div id=auth></div>
<div id=idframe></div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411466.html
標籤:
