0

I'm trying to figure out the best and most clean/concise way to check first if an object even has any values, and if so, if one of those values is 71.

In essence, if the object is empty then the function should return true. Also if the function isn't empty but contains 71 as one of the values (they're numerically indexed) then it should be true. Everything else false.

What I have at the moment works but seems kinda messy and lengthy:

facets is the object

if (Object.keys(facets).length === 0) {
  if (facets[index] == 71) {
    return true;
  } else {
    return false;
  }
}
3
  • 1
    return Object.keys(facets).length == 0 || facets[index] === 71; ? Commented Dec 12, 2016 at 16:07
  • 1
    If they are numerically indexed why not use simple array? Commented Dec 12, 2016 at 16:20
  • 1
    Do you always know the index that should contain 71? Commented Dec 12, 2016 at 16:55

2 Answers 2

1

If you know the index, check if the index contains the value, and if not, check if it's empty:

const has = (obj, index, value) => obj[index] === 71 || !Object.keys(obj).length;

const index = 2;
const value = 71;

console.log('empty: ', has({}, index, value));
console.log('contains 71: ', has({ 1: 13}, index, value));
console.log('contains 71: ', has({ 1: 13, 2: 71 }, index, value));


If you don't know the index, you can use Object#keys to extract the keys, check if length is 0 (!values.length), and if not use Array#findIndex to see the object contains the request value:

const facets = { 1: 0, 2: 71 };

const has = (obj, value) => {
  const keys = Object.keys(obj);
  return !keys.length || keys.findIndex((key) => obj[key] === value) !== -1;
}

const value = 71;

console.log('empty: ', has({}, value));
console.log('contains 71: ', has({ 1: 13}, value));
console.log('contains 71: ', has({ 1: 13, 2: 71 }, value));


Another option when the index is unknown is to use Object#values.

Note: Object#values is not part of ECMAScript2015 (ES6), but a draft in ECMAScript2017, and is supported only by Chrome and Firefox.

Use Object#values to extract the values, check if length is 0 (!values.length), and if not use Array#includes to see if values contain the request value.

const facets = { 1: 0, 2: 71 };

const has = (obj, value) => {
  const values = Object.values(obj);
  return !values.length || values.includes(value);
}

const value = 71;

console.log('empty: ', has({}, value));
console.log('contains 71: ', has({ 1: 13}, value));
console.log('contains 71: ', has({ 1: 13, 2: 71 }, value));

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

1 Comment

Object.values isn't ES6
0

In essence, if the object is empty then the function should return true. Also if the function isn't empty but contains 71 as one of the values (they're numerically indexed) then it should be true. Everything else false.

It is

return !Object.keys(facets).length || facets[index] == 71;

There's no need to explicitly return true or false because comparison and logical operators result in boolean value.

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.