[英]JavaScript array split at odd index
I have an array of products this contains a product name and a manufacturer, eg "product","manufacturer","product","manufacturer"
etc.. 我有一系列产品,其中包含产品名称和制造商,例如
"product","manufacturer","product","manufacturer"
等。
How can I spilt the array into a separate array so that I can have the products and manufacturers in separate arrays? 如何将阵列洒到单独的阵列中,以便将产品和制造商放在单独的阵列中? I want to spilt it at every odd index so that I can take the manufacture out of the array and add this into a new array?
我想将其溢出到每个奇数索引,以便可以将制造产品从阵列中取出并添加到新阵列中吗?
Thanks. 谢谢。
/**
* @param candid Array of results
* @return Returns an array where index 0 = array of even ones, and index 1 = array of odd ones
*/
function splitArray(candid) {
var oddOnes = [],
evenOnes = [];
for(var i=0; i<candid.length; i++)
(i % 2 == 0 ? evenOnes : oddOnes).push(candid[i]);
return [evenOnes, oddOnes];
}
Try with something like this: 尝试这样的事情:
var arr = ["product","manufacturer","product","manufacturer"];
var products = [];
var manufacturers = [];
for (var i = 0; i < arr.length; i += 2) {
products.push(arr[i]);
arr[i+1] && manufacturers.push(arr[i + 1]);
}
Underscore.js groupBy does the work pretty good. Underscore.js groupBy可以很好地完成工作。
_.groupBy(["product", "manufacturer","product", "manufacturer"], function(val, index){ return index % 2; });
=> {1: ["product", "product"], 2: ["manufacturer", "manufacturer"]}
This should work: 这应该工作:
function separate(array) {
var products = [],
manufacturers = [];
for (var i = 0, length = array.length; i < length; i++) {
if (i % 2 === 0) {
products.push(array[i]);
} else {
manufacturers.push(array[i]);
}
}
return {
products: products,
manufacturers: manufacturers
};
}
Edit : my response was a little bit late to the show. 编辑 :我的回应有点晚了。 I'd strongly prefer if you picked one of the other folks who did the same thing.
如果您选择了其他做过同样事情的人,我将非常希望您这样做。
If your array looks like this: 如果您的数组如下所示:
var result = ["Product #1", "ABC Company", "Product #2", "DEF Company"];
You can extract the products and manufacturers using a for
loop 您可以使用
for
循环提取产品和制造商
var products = [];
var manufacturers = [];
for (var i = 0; i < results.length; ++i) {
if (i % 2 === 0) {
products.push(products[i]);
} else {
manufacturers.push(products[i]);
}
}
Use math and the mod operator. 使用数学和mod运算符。
Get thru your array with an index (i, perhaps) and check the value of i 通过索引(可能是i)遍历数组,并检查i的值
if (i % 2 == 0){
goes_to_even_array();
]else{
goes_odd_array();
}
Ok, here's another entry: 好的,这是另一个条目:
var arr = ['p0','m0','p1','m1','p2'];
var products = [];
var manufacturers = [];
var i = 0, iLen = arr.length;
while (i < iLen) {
manufacturers.push(arr[i++]);
products.push(arr[i++]);
}
and if order doesn't matter then: 如果顺序无关紧要,则:
var i = arr.length;
while (i) {
i && manufacturers.push(arr[--i]);
i && products.push(arr[--i]);
}
and if you absolutely trust length to be even: 如果您绝对相信长度是偶数:
var i = arr.length;
while (i) {
manufacturers.push(arr[--i]);
products.push(arr[--i]);
}
but that will go into an endless loop if length is odd. 但是如果长度为奇数,那将陷入无尽的循环。
Another method using recursion :) 使用递归的另一种方法:)
var split = function(arr) {
var splitHelper = function(arr, odd, even) {
if (arr.length == 0) {
return {odd: odd, even: even};
}
odd.push(arr.splice(0, 1)[0]);
arr[0] && even.push(arr.splice(0, 1)[0]);
return splitHelper(arr, odd, even);
};
return splitHelper(arr, [], []);
};
var arr = ["product", "manufacturer", "product", "manufacturer"];
var arrays = split(arr);
// Test
alert("odd: " + arrays.odd);
alert("even: " + arrays.even);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.