Reputation: 35
hopefully you understand my question. Lets say I have this array:
[[1, 0.3], [1, 0.5], [2, 0.6], [2, 0.7], [3, 0.8], [3, 0.9]]
I want new arrays based on the first value like this:
[0.3, 0.5]
[0.6, 0.7]
[0.8, 0.9]
They are three arrays because there are three numbers 1 and 2 and 3. If there are more numbers, then more arrays should come out.
Thanks a lot.
Upvotes: 1
Views: 159
Reputation: 386634
Just use Array.prototype.reduce()
with a value for comparing the group.
var data = [[1, 0.3], [1, 0.5], [2, 0.6], [2, 0.7], [3, 0.8], [3, 0.9]],
array = [];
data.reduce(function (r, a) {
if (r === a[0]) {
array[array.length - 1].push(a[1]);
} else {
array.push([a[1]]);
}
return a[0];
}, null);
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');
Upvotes: 0
Reputation: 1269
If the first element is always non-negative number, something like this also works for you:
function merge(arr){
var tmp = [];
arr.map(function(item){
var key = item[0], value = item[1];
if(!tmp[key]){
tmp[key] = [value];
}
else{
tmp[key].push(value);
}
});
return tmp.filter(function(item){
return item ? true : false;
});
}
http://jsfiddle.net/ndaidong/f1g882gy/
Input: [[1, 0.3], [1, 0.5], [2, 0.6], [2, 0.7], [3, 0.8], [3, 0.9]];
Output: [ [ 0.3, 0.5 ], [ 0.6, 0.7 ], [ 0.8, 0.9 ] ]
Input: [[1, 0.3], [4, 0.6], [3, 0.8], [4, 0.2], [1, 0.5], [2, 0.7], [2, 0.6], [3, 0.9]];
Output: [ [ 0.3, 0.5 ], [ 0.7, 0.6 ], [ 0.8, 0.9 ], [ 0.6, 0.2 ] ]
Upvotes: 0
Reputation: 35
Check for this:
var target = [[1, 0.3], [1, 0.5], [2, 0.6], [2, 0.7], [3, 0.8], [3, 0.9]];
var result = [];
for (var count=0; count<target.length; count++){
var arrayCount = [];
for (var elem in target) {
if (target[elem][0] === count) {
arrayCount.push(target[elem][1]);
}
}
if (arrayCount.length !== 0) {
result.push(arrayCount);
}
}
//This is the result, i.e.: [[0.3, 0.5], [0.6, 0.7], [0.8, 0.9]]
console.log(result);
Upvotes: 0
Reputation: 63524
I agree with the other answerers that an object containing a set of arrays with keys corresponding to the number you want to group by is the way to go. Here's a solution using reduce
. It passes in an empty object as an initial value.
var obj = arr.reduce(function (p, c) {
// for each of the nested arrays we iterate over
// set the key as the "groupBy" number
// (the first element)
var key = c[0];
// if that key doesn't exist in the object (p)
// that we've passed in
// create it and assign a new array to it
p[key] = p[key] || [];
// push the second element of the array to the
// array in the object
p[key].push(c[1]);
// pass the object in to the next iteration
// of the reduce callback
return p;
}, {});
Output
{
"1": [ 0.3, 0.5 ],
"2": [ 0.6, 0.7 ],
"3": [ 0.8, 0.9 ]
}
You can then get the data using square-bracket notation.
obj['1'][0] // 0.3
Upvotes: 0
Reputation: 29836
You can create a new object where the key will be the index number(first index of each current array value) and the value will be the relevant numbers (the second index).
var arr = [[1, 0.3], [1, 0.5], [2, 0.6], [2, 0.7], [3, 0.8], [3, 0.9]];
var newObj = {};
arr.map(function(item){
if(newObj[item[0]])
newObj[item[0]].push(item[1]);
else
newObj[item[0]] = [item[1]];
});
console.log('New object: ' + JSON.stringify(newObj));
// if you insist on creating an array
var newArr = [];
var cnt = 0;
for(var item in newObj)
{
if(newObj.hasOwnProperty(item))
{
newArr[cnt] = newObj[item];
cnt++;
}
}
console.log('New array: ' + JSON.stringify(newArr));
Upvotes: 3
Reputation: 68393
check this fiddle
var arr = [[1, 0.3], [1, 0.5], [2, 0.6], [2, 0.7], [3, 0.8], [3, 0.9]];
var outputObj = {};
var outputArr = [];
for ( var counter = 0; counter < arr.length; counter++ )
{
if ( !outputObj[ arr[ counter ][ 0 ] ] )
{
outputObj[ arr[ counter ][ 0 ] ] = [];
}
outputObj[ arr[ counter ][ 0 ] ].push( arr[ counter ][ 1 ] );
}
for( var id in outputObj )
{
outputArr.push( outputObj[ id ] );
}
console.log( outputArr );
Upvotes: 0