{"action":"created","issue":{"url":"https://api.github.com/repos/wendysel/wendy/issues/3","labels_url":"https://api.github.com/repos/wendysel/wendy/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/wendysel/wendy/issues/3/comments","events_url":"https://api.github.com/repos/wendysel/wendy/issues/3/events","html_url":"https://github.com/wendysel/wendy/issues/3","id":123199174,"number":3,"title":"每日一练 - javascript","user":{"login":"wendysel","id":14126107,"avatar_url":"https://avatars.githubusercontent.com/u/14126107?v=3","gravatar_id":"","url":"https://api.github.com/users/wendysel","html_url":"https://github.com/wendysel","followers_url":"https://api.github.com/users/wendysel/followers","following_url":"https://api.github.com/users/wendysel/following{/other_user}","gists_url":"https://api.github.com/users/wendysel/gists{/gist_id}","starred_url":"https://api.github.com/users/wendysel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wendysel/subscriptions","organizations_url":"https://api.github.com/users/wendysel/orgs","repos_url":"https://api.github.com/users/wendysel/repos","events_url":"https://api.github.com/users/wendysel/events{/privacy}","received_events_url":"https://api.github.com/users/wendysel/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-21T02:25:21Z","updated_at":"2015-12-21T07:14:12Z","closed_at":null,"body":"> 完成该方法，根据传入的对象，返回所有key为id的value\r\n\r\nQuestion：\r\n```javascript\r\nfunction extractIds(data){\r\n    return [];\r\n}\r\n\r\nvar data = {\r\n    id: 1,\r\n    items: [\r\n        {id: 2},\r\n        {id: 3, item: [\r\n            {id: 4},\r\n            {id: 5}\r\n        ]}\r\n    ]\r\n}\r\n\r\nextractIds(data); //should return [1,2,3,4,5];\r\n```\r\n> Solution：\r\n\r\n```javascript\r\n//定义一个空数组\r\nvar arr=[];\r\n//模拟对象\r\nvar data = {\r\n\tid: 1,\r\n\titems: [\r\n\t\t{id: 2},\r\n\t\t{id: 3, items: [\r\n\t\t\t{id: 4},\r\n\t\t\t{id: 5}\r\n\t\t]}\r\n\t]\r\n}\r\n//执行函数\r\nfunction extractIds(data){\r\n\t\r\n\tfor(value in data){\r\n\t\r\n\t\tif(value == 'id'){\r\n\r\n\t\t\tarr.push(data[value]);\r\n\r\n\t\t}else{\r\n\r\n\t\t\textractIds(data[value]);\r\n\t\t}\r\n\t\r\n\t}\r\n\t\r\n\treturn arr;\r\n\t\r\n}\r\n\r\nvar arr = extractIds(data);\r\n\r\nconsole.log(arr);\r\n```\r\n\r\n> Explain：主要是运用递归的知识，并没有太大难度"},"comment":{"url":"https://api.github.com/repos/wendysel/wendy/issues/comments/166219792","html_url":"https://github.com/wendysel/wendy/issues/3#issuecomment-166219792","issue_url":"https://api.github.com/repos/wendysel/wendy/issues/3","id":166219792,"user":{"login":"erbing","id":9959097,"avatar_url":"https://avatars.githubusercontent.com/u/9959097?v=3","gravatar_id":"","url":"https://api.github.com/users/erbing","html_url":"https://github.com/erbing","followers_url":"https://api.github.com/users/erbing/followers","following_url":"https://api.github.com/users/erbing/following{/other_user}","gists_url":"https://api.github.com/users/erbing/gists{/gist_id}","starred_url":"https://api.github.com/users/erbing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erbing/subscriptions","organizations_url":"https://api.github.com/users/erbing/orgs","repos_url":"https://api.github.com/users/erbing/repos","events_url":"https://api.github.com/users/erbing/events{/privacy}","received_events_url":"https://api.github.com/users/erbing/received_events","type":"User","site_admin":false},"created_at":"2015-12-21T07:14:12Z","updated_at":"2015-12-21T07:14:12Z","body":"```javascript\r\n\tfunction extractIds(data){\r\n\r\n    \t\tvar valueArrs = [];\r\n\r\n    \t\tredoit(data);\r\n\r\n    \t\tfunction redoit(data){\r\n\r\n    \t\t\tfor(key in data){\r\n\r\n    \t\t\t\ttypeof data[key]!== \"object\" ? key==='id'&&(valueArrs.push(data[key])): redoit(data[key]);\r\n    \t\t\t}\r\n    \t\t\t\r\n    \t\t}\r\n    \t\t//console.log(keyArrs);\t\r\n    \t\treturn valueArrs;\r\n \t\t\t\t\r\n    \t}\r\n\r\n```"}}