文件的子串
Substring of a file
我有一个结构如下的文件:
var file = "a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d";
现在我将提取此文件的所有字母 "c" 和 "d" 并将这些字母放入数组中,结构如下:
var array = [
[a,b,1],
[a,b,2],
[a,b,3],
[a,b,4],
[a,b,5]
];
我该怎么做?有可能吗?
----------------编辑---------------------
如果我有一个这样结构的数组?
exArray = [
["a":"one", "b":"two", "c":"three", "d":"four"],
["a":"five", "b":"six", "c":"seven", "d":"eight"]
];
新数组必须是:
var array = [
[two,three,1],
[six,seven,2]
];
尝试使用split()
function and map()
function
var file = "a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d";
file.split(',').map(function(el, index) {
var arr = el.split('|');
return [arr[0], arr[1], index+1]
});
如果我理解正确的话,这应该有效:
function transformFile(file) {
return file.split(',').map(function(el) {
return el.split('|'); }
);
}
split()
函数将字符串转换为数组,并将其参数作为项目分隔符。您可以在这里阅读更多相关信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
map()
函数接受一个数组并迭代每个项目,以您在回调函数中定义的方式更改它。这是参考:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map
所以 我们取一个字符串,首先我们将它分成四个数组 - 每个包含 a|b|c|d
字符串。然后 我们将每个字符串再次拆分 (这次使用 |
作为分隔符)将 a|b|c|d
字符串转换为 [a, b, c, d]
数组。所以在这些操作之后,我们最终得到一个数组数组。
要获得您想要的输出,这将起到作用:
var file = "a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d";
var array = file.split(", ") // Break up the original string on `", "`
.map(function(element, index){
var temp = element.split('|');
return [temp[0], temp[1], index + 1];
});
console.log(array);
alert(JSON.stringify(array));
split
将您的 file
字符串转换为这样的数组:
["a|b|c|d", "a|b|c|d", "a|b|c|d", "a|b|c|d", "a|b|c|d"];
然后,在该数组上调用 map
,将每个 "a|b|c|d"
连同它在数组中的位置传递给回调,回调拆分字符串,returns 一个数组包含前 2 个元素,它是 id
(index + 1).
您还可以在 map
中以稍微不同的方式执行回调:
.map(function(element, index){
return element.split('|').slice(0, 2).concat(index + 1);
});
此方法使用相同的拆分,然后使用 slice
to get the first 2 elements from the array, and concat
s 和 id
到具有从 slice
.
返回的 2 个元素的数组
这样,您就不会使用临时变量,那里:
element // "a|b|c|d"
.split('|') // ["a", "b", "c", "d"]
.slice(0, 2) // ["a", "b"]
.concat(index + 1) // ["a", "b", id]
尝试使用 split() 和 replace() 函数。
var file = "a|b|c|d,a|b|c|d,a|b|c|d,a|b|c|d, a|b|c|d";
var NewFile =[];
var i = 1;
file.split(',').forEach(function(el) {
NewFile.push( el.replace("c|d", i).split("|"));
i++;
});
console.log(NewFile);
我有一个结构如下的文件:
var file = "a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d";
现在我将提取此文件的所有字母 "c" 和 "d" 并将这些字母放入数组中,结构如下:
var array = [
[a,b,1],
[a,b,2],
[a,b,3],
[a,b,4],
[a,b,5]
];
我该怎么做?有可能吗?
----------------编辑---------------------
如果我有一个这样结构的数组?
exArray = [
["a":"one", "b":"two", "c":"three", "d":"four"],
["a":"five", "b":"six", "c":"seven", "d":"eight"]
];
新数组必须是:
var array = [
[two,three,1],
[six,seven,2]
];
尝试使用split()
function and map()
function
var file = "a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d";
file.split(',').map(function(el, index) {
var arr = el.split('|');
return [arr[0], arr[1], index+1]
});
如果我理解正确的话,这应该有效:
function transformFile(file) {
return file.split(',').map(function(el) {
return el.split('|'); }
);
}
split()
函数将字符串转换为数组,并将其参数作为项目分隔符。您可以在这里阅读更多相关信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
map()
函数接受一个数组并迭代每个项目,以您在回调函数中定义的方式更改它。这是参考:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map
所以 我们取一个字符串,首先我们将它分成四个数组 - 每个包含 a|b|c|d
字符串。然后 我们将每个字符串再次拆分 (这次使用 |
作为分隔符)将 a|b|c|d
字符串转换为 [a, b, c, d]
数组。所以在这些操作之后,我们最终得到一个数组数组。
要获得您想要的输出,这将起到作用:
var file = "a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d";
var array = file.split(", ") // Break up the original string on `", "`
.map(function(element, index){
var temp = element.split('|');
return [temp[0], temp[1], index + 1];
});
console.log(array);
alert(JSON.stringify(array));
split
将您的 file
字符串转换为这样的数组:
["a|b|c|d", "a|b|c|d", "a|b|c|d", "a|b|c|d", "a|b|c|d"];
然后,在该数组上调用 map
,将每个 "a|b|c|d"
连同它在数组中的位置传递给回调,回调拆分字符串,returns 一个数组包含前 2 个元素,它是 id
(index + 1).
您还可以在 map
中以稍微不同的方式执行回调:
.map(function(element, index){
return element.split('|').slice(0, 2).concat(index + 1);
});
此方法使用相同的拆分,然后使用 slice
to get the first 2 elements from the array, and concat
s 和 id
到具有从 slice
.
返回的 2 个元素的数组
这样,您就不会使用临时变量,那里:
element // "a|b|c|d"
.split('|') // ["a", "b", "c", "d"]
.slice(0, 2) // ["a", "b"]
.concat(index + 1) // ["a", "b", id]
尝试使用 split() 和 replace() 函数。
var file = "a|b|c|d,a|b|c|d,a|b|c|d,a|b|c|d, a|b|c|d";
var NewFile =[];
var i = 1;
file.split(',').forEach(function(el) {
NewFile.push( el.replace("c|d", i).split("|"));
i++;
});
console.log(NewFile);