Home > Software design >  Indicate if a certain element exists through the use of Object.entries ()
Indicate if a certain element exists through the use of Object.entries ()

Time:01-11

I need your help; I have an array of objects (called bookArr, below) as shown below.

bookArr is:

[
  {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}
]

Sometimes, for example, the author is not known; so once I have made a loop for each part, I want to know if the author (always taking the part into consideration) is missing because I need it in the if condition.

Therefore I did as below:

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....
}

I tried to use Object.entries(results)[x][1].part == "" but it doesn't work.

How can I solve my problem?

EDIT

If Author is missing, the entire row corresponding to Author is missing, therefore it does not exist

{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

COMPLETE CODE: (as I said, Author is not always present in the array (therefore the last instruction derives from it))

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  ;
}

CodePudding user response:

If I assume that the array represents just a single book, and you want to know if value is blank for the part: "Author" element or there is no such element, then Object.entries has no role to play here. Instead, use find to find out if there's an element with part: "Author" and, if there is, check that element to see if it has a non-blank 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`
}

Live Example:

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);

CodePudding user response:

If you just wanna check the array contains the part:Author or not, just using Array.find() and you don't need to run the loop.

Updated: doesn't define the x and use Object.entries(results)[1] instead of 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: '', 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}
]

function check(){
let x = 0
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')
    }
    else if(!Object.entries(results)[1].find(item => item.part == 'Author')) {
      console.log(' don"t have author')
      document.getElementById("idframe").style.display="none";
    }
    
x  ;
}
}
check()


//With author
 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: '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();
<div id=img></div>
<div id=title></div>
<div id=txt1></div>
<div id=auth></div>
<div id=idframe></div>

  •  Tags:  
  • Related