0

I would like to detect if some values are not defined in an object with several properties

for example :

let test = {
    helo: undefined,
    hey: "not undefined"
}

i tried this :

const object1 = {
  a: 'somestring',
  b: 42
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

but if possible, I don't want to use a for loop, I would like a boolean result in return

3 Answers 3

3

You could be looking for something like this.

const test = {
    helo: undefined,
    hey: "not undefined"
};

const some_undefined = Object.values(test).some(v => v === undefined);

console.log(some_undefined);

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a function for that :

const hasOneKeyUndefined = (object)=>
{
    for (const [key, value] of Object.entries(object)) {
      if (value === undefined) return true;
    }
    return false;
}

Comments

0

You could also use Object.keys

const test = {
    helo: undefined,
    hey: "not undefined"
};
Object.keys(test).map((key, idx) => test[key] === undefined)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.