0

Hello,

If I have an array structured like the following:

'0' ...
    '0' ...
    '1' ...
    '2' ...
'1' ...
    '0' => "Mike"
    '1' => "1892"
    '2' => "1"
'2' ...
    '0' => "Paul"
    '1' => "1672"
    '2' => "1"
'3' ...
    '0' => "Sam"
    '1' => "1872"
    '2' => "1"
'4' ...
    '0' => "David"
    '1' => "2090"
    '2' => "1"
'5' ...
    '0' => "Daniel"
    '1' => "1652"
    '2' => "1"
'6' ...
    '0' => "Frank"
    '1' => "2192"
    '2' => "0"
'7' ...
    '0' => "Louis"
    '1' => "1892"
    '2' => "1"

   '8' ...
        '0' => "Michael"
        '1' => "1910"
        '2' => "0"

Note: During actual usage, the actual array may be of any size and contain more or less objects than the example provide.

I would like to organize the array in two groups of arrays.
Group one will contain objects where the value of the 3rd key is equal to 0
Group two will contain objects where the value of the 3rd key is equal to 1

Essentially, it needs to look like this:
Group one

  '0' ...
    '0' => "Michael"
    '1' => "1910"
    '2' => "0"
  '1' ...
    '0' => "Frank"
    '1' => "2192"
    '2' => "0"

Group two:

   '0' ...
        '0' => "Louis"
        '1' => "1892"
        '2' => "1"
   '1' ...
        '0' => "Mike"
        '1' => "1892"
        '2' => "1"
    '2' ...
        '0' => "Paul"
        '1' => "1672"
        '2' => "1"
    '3' ...
        '0' => "Sam"
        '1' => "1872"
        '2' => "1"
    '4' ...
        '0' => "David"
        '1' => "2090"
        '2' => "1"

Now from group one, I would like to extract the object where the value of its key 1 is the greater of the group. for Group one, I will need to extract:

  '1' ...
    '0' => "Frank"
    '1' => "2192"
    '2' => "0"

For group two, I would like to extract the object where the value of its key 1 is also the greater of that group. For Group two, I will need to extract:

  '4' ...
    '0' => "David"
    '1' => "2090"
    '2' => "1"

I hope this makes some sense.

I appreciate any thoughts on this!

Thanks,
Mossa

3
Contributors
5
Replies
36
Views
2 Years
Discussion Span
Last Post by mbarandao
0
function getGroups(yourRawArray) {
    var group1 = [];
    var group2 = [];

    for(var i = 0; i < yourRawArray.length; i++) {
        var obj = yourRawArray[i];
        if(obj['2'] == '0') group1.push(obj);
        if(obj['2'] == '1') group2.push(obj);
    }

    return {'group1': group1, 'group2': group2};
}

function getGreatest(groupArray) {
    // To get Greatest you just need to sort the 2 group arrays and pick the first item
    return (groupArray.sort(function(a, b) { return a['2'] > b.['2']; } ))[0];
}


// Get 2 groups
var groups = getGroups(yourRawArray);

// Get Greatest
var greatestOfGroup1 = getGreatest(groups.group1);
var greatestOfGroup2 = getGreatest(groups.group2);
0

I appreciate the response!

I am getting an error that I cannot seem to identify it cause. here is the script implementation:

        var dataparsed ='["Bob","3100",0],["Paul","2293",1],["eric","2099",0],["Carl","1400",1]';


        var data = JSON.parse("[" + dataparsed + "]" );
        function getGroups(data) {
        var group1 = [];
        var group2 = [];
        for(var i = 0; i < data.length; i++) {
        var obj = data[i];
        if(obj['2'] == '0') group1.push(obj);
        if(obj['2'] == '1') group2.push(obj);
        }
        return {'group1': group1, 'group2': group2};
        }
        function getGreatest(groupArray) {
        // To get Greatest you just need to sort the 2 group arrays and pick the first item
        return (groupArray.sort(function(a, b) { return a['2'] > b.['2']; } ))[0];
        }
        // Get 2 groups
        var groups = getGroups(data);
        // Get Greatest
        var greatestOfGroup1 = getGreatest(groups.group1);
        var greatestOfGroup2 = getGreatest(groups.group2);

        alert(greatestOfGroup1);

the error is pointing to the following line

return (groupArray.sort(function(a, b) { return a['2'] > b.['2']; } ))[0];

error type:

    Uncaught SyntaxError: Unexpected token [

any ideas?

Note to diafol,
the array is generated in JS with:

var dataparsed ='["Bob","3100",0],["Paul","2293",1],["eric","2099",0],["Carl","1400",1]';

var data = JSON.parse("[" + dataparsed + "]" );

to printed it to screen i have a js function that mimic php Print_r(), but in JS.

0

Hi Diafol,

This post is as a result of me running into some additional issue after addressing the othe post you mention.

For the question addressed here; essentially, I would like to Group an Array by common values and then extract specific elements from the array based on certain conditions.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.