我有一个数组
var arr = ["2:6", "2:3", "1:1", "1:2", "3:1"];
(这是javascript); 我不知道如何将此数组转换为输出:
["2:6,3", "1:1,2", "3:1"];
你可以帮助我吗?
答案 0 :(得分:3)
看起来您想要将具有相同初始数字的列表元素组合在一起。此代码将为您提供关联数组{"1":"1,2","2":"6,3","3§:"1"}
作为输出:
var arr = ["2:6", "2:3", "1:1", "1:2", "3:1"];
var hist = {};
arr.map( function (a) { b=a.split(":");c=b[0]; if (c in hist) hist[c]=hist[c].concat(","+b[1]); else hist[c] = b[1]; } );
alert(JSON.stringify(hist));
答案 1 :(得分:1)
var interm = {}, output = [], regx = /(\d+):(\d+)/;
arr.forEach(function(x) {var y = regx.exec(x); if (y[1] in interm) interm[y[1]].push(y[2]); else interm[y[1]] = [y[2]]});
Object.keys(interm).map(function(x) { output.push(x + ":" + interm[x].join(',')) });
console.log(output);
[ '1:1,2', '2:6,3', '3:1' ]
这与使用正则表达式和forEach
的速度方面的转换相差甚远,但它相当简洁,你没有提到你需要任何特别快的东西。
答案 2 :(得分:1)
请参阅 LINK..
或申请以下代码......
HTML代码......
<input type='button' id='s' value='test' onclick="test()"/>
JQuery代码......
function test() {
var arr = ["2:6", "2:3", "1:1", "1:2", "3:1"];
var resultStr = "";
for (i = 0; i < arr.length; i++) {
if (i <= arr.length - 2) {
var a = arr[i + 1].split(":");
if (a.length > 0) {
resultStr += arr[i] + ',' + a[1] + ' | ';
}
} else {
var str = arr[i];
resultStr += arr[i];
}
i++;
}
alert(resultStr);
}
答案 3 :(得分:0)
您的输出看起来很像地图。我会把它写成:
{
2 : [ 6, 3 ],
1 : [ 1, 2 ],
3 : [ 1 ]
}
要获取该映射,我将迭代数组,提取键和值,然后将值添加到正确的数组中,确保在尚未创建的情况下创建它。
var arr = ["2:6", "2:3", "1:1", "1:2", "3:1"];
var map = {};
arr.forEach(function(item){
var split = item.split(':');
if (!map[split[0]]) map[split[0]] = [split[1]];
else map[split[0]].push(split[1]);
});
显然,从我的地图中你可以很容易地得到你想要的阵列:
var result = [];
for (var key in map) {
if (map.hasOwnProperty(key)) // best practice
result.push(key+':'+map[key]);
}
console.log(result); // ["1:1,2", "2:6,3", "3:1"]
一个注意事项:它没有你所做的相同顺序的项目,但可以通过迭代原始数组来获取密钥而不是使用for..in来轻松修复:
var result = [];
arr.forEach(function(item){
var key = item.split(':')[0];
if (map[key]) {
result.push(key+':'+map[key]);
delete map[key]; // destroys the map!
}
});
console.log(result); // ["2:6,3", "1:1,2", "3:1"]
解决方案2(无中间地图):
此解决方案具有O(n ^ 2)复杂度:
var arr = ["2:6", "2:3", "1:1", "1:2", "3:1"];
var result = [];
for (var i=0; i<arr.length; i++) {
if (!arr[i]) continue;
var key = arr[i].split(':')[0];
var values = [];
for (var j=i; j<arr.length; j++) {
var split = arr[j].split(':');
if (split[0] === key) {
values.push(split[1]);
arr[j] = undefined; // destroys the original array
}
}
result.push(key + ':' + values);
}
console.log(result); // ["2:6,3", "1:1,2", "3:1"]
答案 4 :(得分:0)
以下是JSFiddle
的工作示例以下示例代码,
var arr = ["2:6", "2:3", "1:1", "1:2", "3:1"];
var tdarray = {};
var newarray = [];
for(var i=0;i<arr.length;i++){
var data = arr[i].split(":");
var found = false;
for(var key in tdarray){
if(data[0]==key){
found = true;
break;
}
}
var list=[];
if(found){
list = tdarray[data[0]];
}
list.push(data[1]);
tdarray[data[0]] = list;
}
for(key in tdarray){
var data = key + ":" + tdarray[key].join();
newarray.push(data);
}
console.log(newarray);
答案 5 :(得分:0)
另一种可能的解决方案......
var arr = ["2:6", "2:3", "1:1", "1:2", "3:1"], alreadyUsedNumbers = [];
for (var i=0; i < arr.length; i++) {
var key = arr[i].split(":")[0], value = arr[i].split(":")[1];
if (alreadyUsedNumbers.indexOf(key) >= 0) {
for (var j=0; j < i; j++) {
if (arr[j].indexOf(key) == 0) {
arr[j] += ","+value;
arr.splice(i, 1)
i--;
break;
}
}
} else {
alreadyUsedNumbers.push(key);
}
}
console.log(arr);
...享受。
答案 6 :(得分:0)
var arr = ["2:6", "2:3", "1:1", "1:2", "3:1", "4:6", "3:4"];
var output = new Array();
var outputString = "[";
for(var i=0; i<arr.length; i++)
{
var index = arr[i].indexOf(":");
var firstNr = arr[i].substring(0,index);
var secondNr = arr[i].substring(index+1,arr[i].length);
var outputPart = firstNr + ":" + secondNr;
var j = i+1;
while (j<arr.length)
{
var index2 = arr[j].indexOf(":");
var firstNr2 = arr[j].substring(0,index2);
var secondNr2 = arr[j].substring(index2+1,arr[j].length);
if (firstNr == firstNr2)
{
outputPart += "," + secondNr2;
arr.splice(j,1);
}
else
j++;
}
output.push(outputPart);
}
for(var k=0; k<output.length; k++)
outputString += "'" + output[k] + "' ";
outputString += "]";
alert(outputString);