Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

check if an object contains an object

I have an object values and I am looping through the object. If a property of the object startsWith(‘FLD_STR_’) and if it has a an object as for value (like the FLD_STR_101), then return an object with its field, fileName, value, type.

here is the sample :

const values ={
  ID:748817,
  CODE:"OFFLINE-003",
  FLD_STR_101:{ field:"FLD_STR_101",
  fileName:"doc.pdf", value:'base64', type:"application/pdf"},
  FLD_STR_102:"coucou",
  Table:109414,
  OWNER:"MJACQUIER",

}


const test = for (const key in values) {
  if(key.startsWith('FLD_STR_') && its value is an object ){
  return {
    field: 'FLD_STR_101',
    fileName: key.fileName,
    value: key.value,
    type: key.type
   } 
  }
 }

console.log(test)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I changed your code slightly, is that what you meant?

const values = {
  ID: 748817,
  CODE: "OFFLINE-003",
  FLD_STR_101: {
    field: "FLD_STR_101",
    fileName: "doc.pdf",
    value: 'base64',
    type: "application/pdf"
  },
  FLD_STR_102: "coucou",
  Table: 109414,
  OWNER: "MJACQUIER",

}


const test = function() {
  for (const key in values) {
    if (key.startsWith('FLD_STR_') && typeof values[key] === 'object') {
      return {
        field: key,
        fileName: values[key].fileName,
        value: values[key].value,
        type: values[key].type
      }
    }
  }
}
console.log(test())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading