我现在很迷茫,我有一份文件:
5632 Hello
545 Thing
6532 Have
以及更多...但我希望得到所有这些数字,并将它们放在这样:
[5632,545,6532]然后如果我想获得更多有关该号码的部门信息,我会选择号码并获得该词
E.g:得到545然后得到它的单词,这是Hello。
也很抱歉我的英语,英语不是我的第一语言。
答案 0 :(得分:1)
示例:
{
"kind": "bigquery#dataset",
"datasetReference": {
"projectId": "PROJECT_ID",
"datasetId": "DATASET_ID"
},
"creationTime": "...",
"access": [
{
"specialGroup": "projectWriters",
"role": "WRITER"
},
{
"role": "WRITER",
"userByEmail": "service_account@PROJECT_ID.iam.gserviceaccount.com"
},
{
"specialGroup": "projectOwners",
"role": "OWNER"
},
{
"role": "OWNER",
"userByEmail": "service_account@PROJECT_ID.iam.gserviceaccount.com"
},
{
"specialGroup": "projectReaders",
"role": "READER"
}
],
"etag": "...",
"lastModifiedTime": "1488658209437",
"id": "PROJECT_ID:DATASET_ID",
"selfLink": "https://www.googleapis.com/bigquery/v2/projects/PROJECT_ID/datasets/DATASET_ID"
}
在你的情况下:
var numberRegex = /\d+/g;
var numbers = '5632 Hello 545 Thing 6532 Have'.match(numberRegex)
console.log(numbers)
// Output:
// ["5632", "545", "6532"]
答案 1 :(得分:0)
首先,您必须操纵字符串并将其转换为正确的数组。然后,您可以使用Array#forEach
对其进行迭代,然后查找您喜欢的所有元素。
var str = "5632 Hello \n545 Thing \n6532 Have",
arr = str.split('\n').map(v => v.trim().replace(/\s+/, ',').split(',')),
numbers = [],
names = [];
arr.forEach(function(v) {
numbers.push(v[0]);
names.push(v[1]);
});
console.log(numbers, names);
答案 2 :(得分:0)
您可以使用正则表达式
假设我们有may行,每行包含两个以空格分隔的值
var text = `5632 Hello
545 Thing
6532 Have`
var result = text.split('\n').map(function(element) {
return element.split(' ')
}).reduce(function(obj, element) {
obj[element[0]] = element[1];
return obj;
}, {});
此代码将返回一个像这样的对象
{545: "Thing", 5632: "Hello", 6532: "Have"}