我有一个lodash变量;
var usernames = _.map(data, 'usernames');
Run Code Online (Sandbox Code Playgroud)
产生以下内容;
[
"joebloggs",
"joebloggs",
"simongarfunkel",
"chrispine",
"billgates",
"billgates"
]
Run Code Online (Sandbox Code Playgroud)
我怎么能调整lodash语句,以便它只返回一个唯一值数组?例如
var username = _.map(data, 'usernames').uniq();
Run Code Online (Sandbox Code Playgroud) 对于Python来说,是否有一个库或类似于lodash的东西?我们在API上广泛使用该库,在我们继续创建一系列Python工作器时,为API语法创建类似的结构是有意义的.
Promise.all当参数传递给每个promise时,如何添加一个promises数组?
例如;
var config = {
name: [
function(val) {
return new Promise(function(resolve, reject) {
resolve('This is ok')
})
},
function(val) {
return new Promise(function(resolve, reject) {
resolve('This is ok')
})
}
],
gender: [
function(val) {
return new Promise(function(resolve, reject) {
resolve('This is ok')
})
},
function(val) {
return new Promise(function(resolve, reject) {
reject('This is NOT ok')
})
}
]
}Run Code Online (Sandbox Code Playgroud)
我基本上想做Promise.all(config[name], ...).这实际上将在一个循环中进行,这样我就可以迭代一个表单对象,传递每个键以获得一个关联的promises数组并将值传递给每个promise.
编辑 小提琴实现了接受的答案,为好奇.
无论如何,一个人可能会转向以下;
{
"ID": "id"
"Name": "name"
}
Run Code Online (Sandbox Code Playgroud)
成;
{
"id": "ID",
"name": "Name"
}
Run Code Online (Sandbox Code Playgroud)
用lodash?我特意寻找的东西;
var newObj = _.reverseMap(oldObj);
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
好的,我正在尝试使用请求模块向API端点发出两个或更多请求.我正在渲染一个HTML文件,并使用以下代码将返回的JSON传递给把手模板:
res.render('list.html', {
title: 'List',
data: returnedJSON
}
Run Code Online (Sandbox Code Playgroud)
然后我可以很容易地在我的把手模板中迭代这个JSON.
我遇到的问题是,我现在需要使用多个数据源,其中类别列表将根据JSON响应类别和员工JSON响应中的Staff列表构建.我想要一个简单的解决方案,我可以做到这一点,但扩展它使用任意数量的数据源.
下面是我对一个数据源的完整代码片段:
request({
url: 'https://api.com/categories',
headers: {
'Bearer': 'sampleapitoken'
}
}, function(error, response, body) {
if(error || response.statusCode !== 200) {
// handle error
} else {
var json = JSON.parse(body);
res.render('list.html', {
title: 'Listing',
data: json
});
}
});
Run Code Online (Sandbox Code Playgroud)
这适用于一个端点,但如前所述,我现在需要使用多个请求并拥有多个数据源,例如:
request({
url: ['https://api.com/categories','https://api.com/staff'],
headers: {
'Bearer': 'sampleapitoken'
}
}, function(error, response, body1, body2) {
if(error || response.statusCode !== 200) {
// handle error
} else {
var json1 = JSON.parse(body1); …Run Code Online (Sandbox Code Playgroud) 通过以下路线:
app.get('/', controller.web.Home);
Run Code Online (Sandbox Code Playgroud)
如何添加'/'允许匹配的内容/,/index以及/index.html?我还想对所有其他路由使用此方法,以便用户在将.html添加到路径时看不到错误页面.
我在Express网站上看到过这个,但是对于匹配的倍数没有明确的解释.提前致谢.
我正在尝试创建一个非常简单的 shell 脚本来检测它是否以root. 出于某种原因,下面的代码Error: [: stackunderflow: unexpected operator在第 6 行产生了一个,我不知道为什么?我有一个 bash 脚本,但为了可移植性和一些教育,我想用一个 shell 脚本来做这个。
#!/bin/sh
##
# Check for root launch
#
set -e
USER="$(id -un 2>/dev/null || true)"
if [ "$USER" == 'root' ]; then
echo "You must be a root user"
exit 1
else
echo 'Not root!'
exit 0
fi
Run Code Online (Sandbox Code Playgroud)
这是./check.sh从 Debian Jessie amd64 桌面运行的。
我有以下结构:
var output = [{
"article": "BlahBlah",
"title": "Another blah"
}, {
"article": "BlahBlah",
"title": "Return of the blah"
}, {
"article": "BlahBlah2",
"title": "The blah strikes back"
}, {
"article": "BlahBlah2",
"title": "The blahfather"
}]
Run Code Online (Sandbox Code Playgroud)
从上面使用优雅的lodash单线开始,我需要创建以下结构。
var newOutput = [{
"article": "BlahBlah",
"titles": ["Another blah", "Return of the blah"]
}, {
"article": "BlahBlah2",
"titles": ["The blah strikes back", "The blahfather"]
}]
Run Code Online (Sandbox Code Playgroud)
非常感谢您一如既往地提供帮助。.对于解决方案的工作原理的解释非常有用。
我正在尝试动态生成一个FormBuilder组,并且被生成的组对象所困扰。我有以下用于单个.html输入字段的组件。
import { Component, onInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
job: any;
constructor(
public formBuilder: FormBuilder
) {
this.job = {
number: 'J001'
}
}
ngOnInit() {
const jobGroup: FormGroup = new FormGroup({});
for (const key in this.job) {
if (this.job.hasOwnProperty(key)) {
const control: FormControl = new FormControl(this.job[key], Validators.required);
jobGroup.addControl(this.job[key], control);
}
}
this.jobForm = this.formBuilder.group(jobGroup);
}
} …Run Code Online (Sandbox Code Playgroud) 使用诸如uuid如何生成以下结构的模块。
[
'd9baea4e-2436-4481-accb-7c2fe835039e',
'60152666-8c2c-4d33-a5c8-da1dda106c5d',
'c4eaa558-83cc-4b94-9aff-1aefdc204794',
'273998e3-3138-41eb-b740-28ee53f7e344'
]
Run Code Online (Sandbox Code Playgroud)
我是否正确地假设这可以通过Promise一种方法来完成,或者至少可以给出这样的方法。
我有一个方法需要从提供的id参数返回用户对象.其他方法将使用此结构,因此不是使用现有结构的简单情况.
基本上我有一个包含type密钥的数据库,这是过滤的,所以我有一个集合users.我需要做的是创建一个由用户_id值键入的对象,并包含带有_id和type省略的用户对象.
使用lodash或一些ES6糖我想改变以下内容Array;
[
{
"_id": "0e12e661cb50068a135b36067f001d20",
"name": "Joe Bloggs",
"type": "user"
},
{
"_id": "0e12e661cb50068a135b36067f00373f",
"name": "Ben Bloggs",
"type": "user"
}
]
Run Code Online (Sandbox Code Playgroud)
进入下面Object,用_id和type省略;
{
"0e12e661cb50068a135b36067f001d20": {
"name": "Joe Bloggs"
},
"0e12e661cb50068a135b36067f00373f": {
"name": "Ben Bloggs"
}
}
Run Code Online (Sandbox Code Playgroud)
编辑我需要返回对象而不是名称,因为可以将更多属性添加到这些对象中.