【问题标题】:Indicate if a certain element exists through the use of Object.entries ()通过使用 Object.entries() 指示某个元素是否存在
【发布时间】:2022-01-10 16:36:15
【问题描述】:

我需要你的帮助;我有一个对象数组(称为 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++;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!如果您还没有,请使用tour(您将获得徽章!),四处看看,并通读help center,尤其是How do I ask a good question? 我还推荐Jon Skeet 的Writing the Perfect QuestionQuestion Checklist .
  • 恐怕问题中的信息太零碎,我们无法提供帮助。您谈论的是数组,但随后您使用的是Object.entries,它适用于非数组对象(尽管它适用于数组,因为数组也是对象)。请使用 minimal reproducible example 来更新您的问题,以展示问题,最好是使用 Stack Snippets([&lt;&gt;] 工具栏按钮)的 runnable 问题; here's how to do one.
  • @T.J.Crowder 对象数组
  • 我知道,但同样,你不会使用Object.entries。我们需要更多的上下文。你是按条目做的吗?还是整个阵列只用一次?还是……?
  • 为什么会这样?与其他字段一起正常工作。我不明白为什么 Object.entries (results) [x] [1] .part == "" (just put "") 不起作用

标签: javascript loops


【解决方案1】:

如果您只想检查数组是否包含part:Author,只需使用Array.find(),您无需运行循环。

更新:未定义 x 并使用 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>

【讨论】:

  • 我再告诉你一件事。如果一本书有作者,则两篇文章都会显示在控制台上。这样 if 语句总是被读取
  • @a_l_e_x,不,这不是真实的声明,请再次检查。它使auth 被显示:无
  • 是的,我明白你的意思。但不幸的是,该程序似乎这样做了。没有什么办法可以解决这个问题了?
  • @a_l_e_x,我想是的,我们已经讨论了 30 多行大声笑,我认为它应该是绝对有效的(在我这边工作)。我尽力了:(不知道为什么不为你工作......这是我第一次花这么多时间与人们谈论特定主题。哈哈。希望我的回答在某些情况下对你有所帮助!
  • 完美,无限感谢。只要我有机会投票给答案,我就会这样做,因为您非常乐于助人和慷慨。我永远不会停止感谢你。感谢您的耐心等待。
【解决方案2】:

如果我假设数组只代表一本书,并且您想知道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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 2015-04-25
    • 2020-08-13
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多