最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if properties of a TypeScript object are all empty - Stack Overflow

programmeradmin0浏览0评论

I have an object that looks like this:

objectProp = {
  property1: [],
  property2: [],
}

I want to check if all properties(that are string arrays) if this object are null and return true. I am for some reason stuck. How can I do this? I am using "target": "es2015"

I tried this but does not work:

 if (!Object.keys(this.objectProp).every(array => array.length > 0)) {
      console.log('all are empety');
    }

I have an object that looks like this:

objectProp = {
  property1: [],
  property2: [],
}

I want to check if all properties(that are string arrays) if this object are null and return true. I am for some reason stuck. How can I do this? I am using "target": "es2015"

I tried this but does not work:

 if (!Object.keys(this.objectProp).every(array => array.length > 0)) {
      console.log('all are empety');
    }
Share Improve this question edited Feb 25, 2020 at 14:59 Kathrine Hanson asked Feb 25, 2020 at 14:36 Kathrine HansonKathrine Hanson 6154 gold badges11 silver badges33 bronze badges 1
  • Does this answer your question? How to Check if Arrays in a Object Are All Empty? – Heretic Monkey Commented Feb 25, 2020 at 14:59
Add a ment  | 

4 Answers 4

Reset to default 6

You need to use Object.values(this.objectProp) instead of Object.keys(this.objectProp) for this to work. As Object.keys method returns an array of a given object's own enumerable property names and Object.values method returns an array of a given object's own enumerable property values, which is actually what you are looking for.

let objectProp = {
  property1: [],
  property2: [],
}

console.log(Object.keys(objectProp))

console.log(Object.values(objectProp))

For Es2015, you can try this:

let obj = {
  property1: [],
  property2: [],
}

// Log is print only when all arrays are empty
if (Object.keys(obj).map(e => obj[e]).every(a => a.length === 0)) {
  console.log('all are empty');
}

By using Object.keys() you can do it as following

var objectProp = {
  property1: [],
  property2: [],
}

if(Object.keys(objectProp).every(key => objectProp[key] && objectProp[key].length === 0)){
  console.log('All is empty')
}

You should use Object.values as follow:

if (!Object.values(this.objectProp).every(({length}) => Boolean(length))) {
  console.log('all are empety');
}

Most answers seem to have missed the part about your logic been slightly wrong. They will say empty, if only single item is empty,. This is because the logic is !every > 0 But switching this logic to every <= 0 will get what you want.

eg.

The below example will return true, true for the broken version. But true, false for the fixed one.

var objectProp = {
  property1: [],
  property2: [],
};

var objectProp2 = {
  property1: [],
  property2: [],
  property3: ['not empty']
};


function allIsEmptyBroken(objectProp) {
  return (!Object.keys(objectProp).every(key => objectProp[key] && objectProp[key].length > 0));
}

function allIsEmptyFixed(objectProp) {
  return (Object.keys(objectProp).every(key => objectProp[key] && objectProp[key].length <= 0));
}


console.log('allIsEmptyBroken');
console.log(allIsEmptyBroken(objectProp));
console.log(allIsEmptyBroken(objectProp2));

console.log('allIsEmptyFixed');
console.log(allIsEmptyFixed(objectProp));
console.log(allIsEmptyFixed(objectProp2));

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>