Home > Enterprise >  check if an object contains an object
check if an object contains an object

Time:07-21

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)

CodePudding user response:

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

  • Related