小编Mor*_*len的帖子

在对象列表上使用.reduce()

我有这个样本数据集:

let list = [
  {'first': 'morgan', 'id': 1},
  {'first': 'eric', 'id': 1},
  {'first': 'brian', 'id': 2 },
  {'first': 'derek', 'id' : 2},
  {'first': 'courtney', 'id': 3},
  {'first': 'eric', 'id': 4},
  {'first': 'jon', 'id':4},
]
Run Code Online (Sandbox Code Playgroud)

我想结束这个:

[[1, [morgan, eric]], [2, [brian, derek]], [3, [courtney]], [4, [eric, jon]]
Run Code Online (Sandbox Code Playgroud)

我正在使用该.reduce()函数来映射列表.但是,我有点卡住了.

我有这个工作:

let b = list.reduce((final_list, new_item) => {
  x = final_list.concat([[new_item.id, [new_item.first]]])
  return x
}, [])
Run Code Online (Sandbox Code Playgroud)

但是,这会将列表展平为列表列表,但不会合并共享相似ID的名称.

我尝试使用.map() 下面的代码不起作用

我试图映射到final_list(这是[id,[names]]的列表,看看是否new_item存在的id smaller_list然后添加new_item.firstsmaller_list[1](应该是名称列表).

这是正确的方法吗? …

javascript

4
推荐指数
1
解决办法
177
查看次数

获取获取响应数据

我正在使用 chrome devTools 来镜像 webRequest。查看网络请求,响应中有一些我想要访问的 JSON 数据

\n\n

右键单击 --> 复制为获取 -->

\n\n
fetch(\n    "https://www.url.com/service.svc?action=FindConversation&ID=-40&AC=1",\n    {\n        "credentials":"include",\n        "headers":{\n            "accept":"*/*",\n            "accept-language":"en-US,en;q=0.9",\n            "action":"FindConversation",\n            "content-type":"application/json; charset=UTF-8",\n            "actionid":"-40",\n            "unique_identifier":"062lCufCY0i5mI9NMTRUsF87XDq9ttYIonzZQjBcCOPvzoIJFOTSI6ZVNK9lMwy_iPFY2tuZzPY."\n            "x-requested-with":"XMLHttpRequest"\n        },\n        "referrer":"https://ballard.amazon.com/OWA/",\n        "referrerPolicy":"no-referrer-when-downgrade",\n        "body":"contains some body data I want to manipulate",\n        "method":"POST",\n        "mode":"cors"\n    }\n).then(res => {console.log(res)})\n
Run Code Online (Sandbox Code Playgroud)\n\n

这会打印出类似这样的内容:

\n\n
Response {type: "basic", url: "https://url/service.svc?action=FindConversation&ID=-40&AC=1", redirected: false, status: 200, ok: true, \xe2\x80\xa6}\nbody: ReadableStream\nlocked: false\n__proto__: ReadableStream\nbodyUsed: false\nheaders: Headers {}\nok: true\nredirected: false\nstatus: 200\nstatusText: "OK"\ntype: "basic"\nurl: "https://url/OWA/service.svc?action=FindConversation&ID=-40&AC=1"\n__proto__: Response\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我检查刚刚发出的网络请求时,它看起来没有返回任何 JSON 数据,而是用代码进行响应200。这正常吗?

\n\n

我要么期望它返回 JSON 数据,要么失败。 …

javascript fetch-api

4
推荐指数
1
解决办法
8946
查看次数

导出路径不保存

我正在尝试安装 Google SDK

我输入:

export PYTHONPATH=$PYTHONPATH:/Users/morganallen/Dropbox/google_appengine

然后:

echo $PATH

我看到:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/morganallen/Dropbox/google_appengine

但是当我退出终端并重新打开它并键入时,echo $PATH我只看到:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

为什么不节省呢?

我看到其他答案提到了一个.bashrc文件,但我找不到我的?不知道该怎么办。

bash

3
推荐指数
1
解决办法
6898
查看次数

jQuery POST:将Content-Type设置为text / plain

我不知道为什么这不起作用。在服务器上,我有这个:

@app.route('/message', methods=['POST'])
def print_post():
    if request.headers['Content-Type'] == 'text/plain':
        logging.warning(request.data)
        return "Text Message: " + request.data
    else:
        logging.warning('didnt work')
        return 'Unsupported Media Type'
Run Code Online (Sandbox Code Playgroud)

我正在通过浏览器发送此请求:

$.ajax({
    type: "POST",
    url: "https://localhost:8090/message",
    data: 'this is a message'
    contentType: 'test/plain'
})
Run Code Online (Sandbox Code Playgroud)

但我不断收到错误 Uncaught SyntaxError: Unexpected identifier

我究竟做错了什么?

jquery

3
推荐指数
1
解决办法
8845
查看次数

将其传递给函数的Javascript

我不习惯this和尝试使一些简单的函数来回传递它。我不太确定javascript期望什么,但是我认为我做的不正确。

$(".search-result").each(function() {
    var x  = $(this).find('.past-positions ol').children()
    console.log(x)
    //this prints as expected
    pastJobs(this)
    // this does not
})

function pastJobs() {
    var x  = $(this).find('.past-positions ol').children()
    console.log(x)
    // this prints as undefined
}
Run Code Online (Sandbox Code Playgroud)

我认为它可以传递this给函数,但是我认为我做的方法不正确。

我究竟做错了什么?

javascript variables this

3
推荐指数
1
解决办法
574
查看次数

JS 中的解码 URL 没有按预期工作

我有这个我试图解码的网址

var a = 'https%3A%2F%2Fwww.gem.com%2Fextension%3Furl%3D'
var b = decodeURI(a)
console.log(b)
Run Code Online (Sandbox Code Playgroud)

"https%3A%2F%2Fwww.gem.com%2Fextension%3Furl%3D"

我究竟做错了什么?

javascript

3
推荐指数
1
解决办法
23
查看次数

使用 Beautifulsoup 在 html 页面中查找 CSRF 令牌

HTML 看起来像这样

<input type="hidden" name="csrfToken" value="ajax:SOME_TOKEN"/>
Run Code Online (Sandbox Code Playgroud)

我尝试了几种不同的方法,但总是出现错误。我认为这种方式看起来是正确的,但显然不是。

soup = BeautifulSoup(html_page)
soup.find('input', {'name':'csrfToken'})
Run Code Online (Sandbox Code Playgroud)

我不断得到:

TypeError: 'expected string or buffer'
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

python beautifulsoup

2
推荐指数
1
解决办法
7480
查看次数

遇到格式化程序的问题:"%支持的操作数类型:'NoneType'和'tuple'"

我收到这个错误,我不明白为什么.

我正在学习使用Gmail的API并复制粘贴他们的示例:https://developers.google.com/gmail/api/v1/reference/users/threads/get#examples

这是代码:

def GetThread(service, user_id, thread_id):
  """Get a Thread.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    thread_id: The ID of the Thread required.

  Returns:
    Thread with matching ID.
  """
  try:
    thread = service.users().threads().get(userId=user_id, id=thread_id).execute()
    messages = thread['messages']
    print ('thread id: %s - number of messages '
           'in this thread: %d') % (thread['id'], len(messages))
    return thread
  except errors.HttpError, error:
    print 'An …
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
277
查看次数

我应该如何将 Redux 添加到使用 Create-React-App 创建的 React 项目中?

我使用 Create-React-App 创建了一个 React 项目。该package.json文件显示:

{
  "name": "dashboard",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.5.4",
    "react-dom": "^15.5.4"
  },
  "devDependencies": {
    "react-scripts": "1.0.7"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}
Run Code Online (Sandbox Code Playgroud)

如何添加 Redux?我需要运行什么命令才能将其安装到我的 node_modules 中?

reactjs

2
推荐指数
1
解决办法
884
查看次数

类型错误:gapi.auth2.grantOfflineAccess 不是函数

我正在gapi.auth2我的 index.html 中初始化

<!DOCTYPE html>
<html lang="en">
    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="google-signin-client_id" content= "817677528939-dss5sreclldv1inb26tb3tueac98d24r.apps.googleusercontent.com">

        <meta name="google-signin-scope" content="profile email">
        <script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
        <script>
            function start() {
                console.log('script running')
                gapi.load('auth2', function() {
                    auth2 = gapi.auth2.init({
                        client_id: '817677528939-dss5sreclldv1inb26tb3tueac98d24r.apps.googleusercontent.com',
                        scope: 'profile email'
                    });
                });
            }
        </script>

    <!-- <meta name="theme-color" content="#000000">     -->
    <!-- <link rel="manifest" href="%PUBLIC_URL%/manifest.json"> -->
    <!-- <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> -->

    <title>React App</title>
  </head>
  <body>
    <div id="root"></div>


  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

然后在我的反应应用程序中:

在页面顶部: /* global gapi */ …

javascript reactjs

2
推荐指数
1
解决办法
2120
查看次数