小编Ben*_*174的帖子

ES6映射一个对象数组,以返回带有新键的对象数组

我有一个对象数组:

[
    {
        id: 1,
        name: 'bill'
    },
    {
        id: 2,
        name: 'ted'
    }
]
Run Code Online (Sandbox Code Playgroud)

寻找一个简单的单行返回:

[
    {
        value: 1,
        text: 'bill'
    },
    {
        value: 2,
        text: 'ted'
    }
]
Run Code Online (Sandbox Code Playgroud)

因此,我可以使用正确的键轻松地将它们泵入反应下拉列表中.

我觉得这个简单的解决方案应该可行,但我得到的语法错误无效:

this.props.people.map(person => { value: person.id, text: person.name })
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6

117
推荐指数
1
解决办法
13万
查看次数

无法从 jQuery 检索发布到 Django 的对象数组

我的Javascript:

var postData = {
    customer: 'test', 
    order: 1, 
    boxes: [
        {
            "size":"2",
            "color":"1",
            "colorNumber":"1",
            "barCode":"1234567890",
            "barCodePic":"",
        },
        {
            "size":"3",
            "color":"1",
            "colorNumber":"2",
            "barCode":"0987654321",
            "barCodePic":"",
        }
    ]
}

jQuery.post("http://10.0.1.7:8001/bapi/order/", postData );
Run Code Online (Sandbox Code Playgroud)

我的蟒蛇:

print 'Customer:', request.POST.get('customer', None)                   
print 'Order:', request.POST.get('order', None)                         
print 'get - boxes:', request.POST.get('boxes', None)                    
print 'get - boxes[]:', request.POST.get('boxes[]', None)               
print 'getlist - boxes[]:', request.POST.getlist('boxes[]')             
print 'getlist - boxes:', request.POST.getlist('boxes')                                                                      
print request.POST                                                      
Run Code Online (Sandbox Code Playgroud)

输出:

Customer: test
Order: 1
get - boxes: None
get - boxes[]: None
getlist - boxes[]: [] …
Run Code Online (Sandbox Code Playgroud)

javascript python django jquery

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

ViewSet的总计结果导致Django Rest Framework

我有一个ViewSet,它输出一组结果:

{
    "count": 19355,
    "next": null,
    "previous": null,
    "results": [
        {
            "duration": 5,
            ...
        },
        {
            "duration": 5,
            ...
        },
        ...
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想汇总结果持续时间的总和,就在"计数"旁边那个方便的小顶部区域.我知道如何使用annotate和sum在查询集中执行此操作,但我没有看到将其放入ViewSet输出的方法.

此数据集的所需输出将是:

{
    "count": 19355,
    "total_duration": 10,
    "next": null,
    "previous": null,
    "results": [
        {
            "duration": 5,
            ...
        },
        {
            "duration": 5,
            ...
        },
        ...
    ]
}
Run Code Online (Sandbox Code Playgroud)

我很感激帮助!

django django-rest-framework

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