Javascript 在javascript拆分函数中拆分数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25967358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
splitting array elements in javascript split function
提问by Stack s
Hi i have the below array element
嗨,我有以下数组元素
var array =["a.READ","b.CREATE"]
var 数组 =["a.READ","b.CREATE"]
I'm trying to split the elements based on "." using javascript split method
我正在尝试根据“.”拆分元素。使用javascript拆分方法
below is my code
下面是我的代码
var array1=new Array();
var array2 = new Array();
for (var i = 0; i < array .length; i++) {
array1.push(array [i].split("."));
}
console.log("this is the array1 finish ----"+array1)
The out put that i'm receiving is
我收到的输出是
[["a","READ"],["b","CREATE"]]
The expected output that i want is
我想要的预期输出是
array1 =["a","b"]
array2=["READ","CREATE"]
I'm stuck here any solution regarding this is much helpful
我被困在这里任何关于此的解决方案都非常有帮助
回答by Adam
You need to add to array2 and use both elements from the returned array that String.prototype.splitreturns - i.e. 0 is the left hand side and 1 is the right hand side of the dot.
您需要添加到 array2 并使用String.prototype.split返回的返回数组中的两个元素- 即 0 是点的左侧,1 是点的右侧。
var array = ["a.READ", "b.CREATE"]
var array1 = []; // better to define using [] instead of new Array();
var array2 = [];
for (var i = 0; i < array.length; i++) {
var split = array[i].split("."); // just split once
array1.push(split[0]); // before the dot
array2.push(split[1]); // after the dot
}
console.log("array1", array1);
console.log("array2", array2);
回答by Stack s
We'll start off with a generic transposefunction for two-dimensional arrays:
我们将从transpose二维数组的通用函数开始:
function transpose(arr1) { // to transpose a 2d array
return arr1[0].map( // take the first sub-array and map
function(_, i) { // each element into
return arr1.map( // an array which maps
function(col) { // each subarray into
return col[i]; // the corresponding elt value
}
);
}
);
}
Now the solution is just
现在的解决办法就是
transpose( // transpose the two-dimensional array
array.map( // created by taking the array and mapping
function(e) { // each element "a.READ" into
return e.split('.'); // an array created by splitting it on '.'
}
)
)
回答by Ian Paulo Perina
You can create an Object like this:
您可以像这样创建一个对象:
let temp_ = document.cookie.split(";");
let cookies = {};
for(x = 0; x < temp_.length; x++){
cookies[temp_[x].split("=")[0]] = temp_[x].split("=")[1]
}
and access it:
并访问它:
let value = cookies.key;
EDIT: Just saw the topic's date now, sorry
编辑:现在才看到主题的日期,抱歉
回答by Pramod S. Nikam
You are adding nothing to array2. Please use indexes properly , like below:
您没有向 array2 添加任何内容。请正确使用索引,如下所示:
var array1=new Array();
var array2 = new Array();
for (var i = 0; i < array .length; i++) {
array1.push(array [i].split(".")[0]);
array2.push(array [i].split(".")[1]);
}
回答by Prabhu Murthy
you can do something like this
你可以做这样的事情
var array =["a.READ","b.CREATE"];
var arr1= [], arr2= [];
array.forEach(function(item,index,arr){
item.split('.').forEach(function(item,index,arr){
if(index % 2 === 0){
arr1.push(item);
}else{
arr2.push(item);
}
});
});
console.log(arr1);
console.log(arr2);
回答by undefined
I guess this is a bit redundant but, the split method actually returns and array. Although your code was off you were not modifying array2. Consider the following.
我想这有点多余,但是 split 方法实际上返回和数组。尽管您的代码已关闭,但您并未修改 array2。考虑以下。
var array = [ "a.READ" , "b.CREATE" ]
, array1 = []
, array2 = []
// cache array length
, len = array.length;
for ( var i = 0; i < len; i++ ) {
// the split method returns a new array
// so we will cache the array
// push element 0 to array1
// push element 1 to array2
var newArr = array[ i ].split('.');
array1.push( newArr[ 0 ] );
array2.push( newArr[ 1 ] );
}
console.log( 'array1: ', array1 );
console.log( 'array2: ', array2 );
回答by sadrzadehsina
Use this:
用这个:
for (var i = 0; i < array .length; i++) {
var parts = array[i].split('.');
array1.push(parts[0]);
array2.push(parts[1]);
}
回答by mrsrizan
You have not assigned any value to Array2. You can do as shown below.
您尚未为 Array2 分配任何值。您可以按照下图进行操作。
var array1=[];
var array2 = [];
for (var i = 0; i < array .length; i++) {
var arrayTemp=[];
arrayTemp.push(array [i].split("."));
array1.push(arrayTemp[0]);
array2.push(arrayTemp[1]);
}

