Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尽量不要在JSON中使用null或未定义的值来使用Object.keys函数_Json_Typescript_Typeof - Fatal编程技术网

尽量不要在JSON中使用null或未定义的值来使用Object.keys函数

尽量不要在JSON中使用null或未定义的值来使用Object.keys函数,json,typescript,typeof,Json,Typescript,Typeof,我有一个JSON,看起来像: { 'total_count': 6, 'incomplete_results': false, 'items': [ { 'url': 'https://api.github.com/repos/Samhot/GenIHM/issues/2', 'repository_url': 'https://api.github.com/repos/Samhot/GenIHM', 'comments_url': 'https://api.gith

我有一个JSON,看起来像:

{
'total_count': 6,
'incomplete_results': false,
'items': [
  {
    'url': 'https://api.github.com/repos/Samhot/GenIHM/issues/2',
    'repository_url': 'https://api.github.com/repos/Samhot/GenIHM',
    'comments_url': 'https://api.github.com/repos/Samhot/GenIHM/issues/2/comments',
    'events_url': 'https://api.github.com/repos/Samhot/GenIHM/issues/2/events',
    'html_url': 'https://github.com/Samhot/GenIHM/issues/2',
    'id': 293234257,
    'number': 2,
    'title': 'Créer serveur pour API RESTful',
    'user': {
      'login': 'Samhot',
      'id': 7148311,
      'avatar_url': 'https://avatars3.githubusercontent.com/u/7148311?v=4',
      'gravatar_id': '',
      'url': 'https://api.github.com/users/Samhot',
      'html_url': 'https://github.com/Samhot',
      'followers_url': 'https://api.github.com/users/Samhot/followers',
      'subscriptions_url': 'https://api.github.com/users/Samhot/subscriptions',
      'organizations_url': 'https://api.github.com/users/Samhot/orgs',
      'repos_url': 'https://api.github.com/users/Samhot/repos',
      'received_events_url': 'https://api.github.com/users/Samhot/received_events',
      'type': 'User',
      'site_admin': false
    },
    'state': 'open',
    'locked': false,
    'assignee': null,
  }
 ]
};
我尝试使用
getDeepKeys()
函数从这个JSON获取所有密钥:

getDeepKeys2(obj) {
  const keys = Object.keys(obj);
  const childKeys = keys
    .map(key => obj[key])
    .map(
      value =>
        Array.isArray(value)
          ? this.getDeepKeys2(value[0]) : typeof value === 'object'
            ? this.getDeepKeys2(value) : []
    )
    .reduce((acc, keys) => [...acc, ...keys], []);
  this.dispotest = [...keys, ...childKeys];
  return this.dispotest;
}
只要JSON不包含null或未定义的值(如
“受让人”:null

如果此类型的值存在于我的JSON中,则函数返回:

错误类型错误:无法将未定义或null转换为对象

我想使用
typeof
函数来执行
if(typeof value==null{return null;}
,但我不知道我必须在哪里应用这个


谢谢你的帮助!

我的直觉是你的尝试:

if (typeof value === null { return null; }
只是需要:

if (value == null) { return null; }
注意:双重
==
是故意的,因为这将同时处理
null
undefined

完整示例:

function getDeepKeys2(obj) {
  const keys = Object.keys(obj);
  const childKeys = keys
    .map(key => obj[key])
    .map(value => {
      if (value == null || (value.length != null && value.length === 0)) {
        return null;
      }
      return Array.isArray(value)
        ? this.getDeepKeys2(value[0]) : typeof value === 'object'
        ? this.getDeepKeys2(value) : []
    })
    .reduce((acc, keys) => [...acc, ...keys], []);
  this.dispotest = [...keys, ...childKeys];
  return this.dispotest;
}
输出(在控制台中):


拜托@Fenton你能看看我的最后一个问题吗?Thanks@samhot更新以处理空数组…此外,我需要删除您的答案,因为这是一个问题,而不是一个答案。非常感谢,它很有效!我删除了我的答案抱歉
(29) […]
0: "total_count"
1: "incomplete_results"
2: "items"
3: "url"
4: "repository_url"
5: "comments_url"
6: "events_url"
7: "html_url"
8: "id"
9: "number"
10: "title"
11: "user"
12: "state"
13: "locked"
14: "assignee"
15: "login"
16: "id"
17: "avatar_url"
18: "gravatar_id"
19: "url"
20: "html_url"
21: "followers_url"
22: "subscriptions_url"
23: "organizations_url"
24: "repos_url"
25: "received_events_url"
26: "type"
27: "site_admin"
28: null
length: 29
__proto__: Array []