JsusSalv
JsusSalv

Reputation: 517

Split JavaScript array into smaller ones and get frequency of each value

I have the following big array:

var divs = [
    {class:'A', top:0,   left:0},
    {class:'B', top:50,  left:60},
    {class:'C', top:30,  left:10},
    {class:'D', top:100, left:180},
    {class:'E', top:80,  left:50},
    {class:'F', top:100, left:200},
    {class:'G', top:50,  left:80}
];

I'd like to isolate just the top values. I tried using split() but I might not be using it correctly because nothing returns as expected.

Once the top values are isolated into their own smaller array I want to iterate over it and find the frequency of occurrence for each value.

Can I please get some help?

Upvotes: 1

Views: 324

Answers (5)

frosty
frosty

Reputation: 21762

var divs = [
            {class:'A', top:0,   left:0},
            {class:'B', top:50,  left:60},
            {class:'C', top:30,  left:10},
            {class:'D', top:100, left:180},
            {class:'E', top:80,  left:50},
            {class:'F', top:100,  left:200},
            {class:'G', top:50,  left:80}
];
var topsy = {};
for(var i = 0, max = divs.length; i < max; i++){
    var a = divs[i];
    topsy[a.top] = (topsy[a.top] + 1) || 1;
}

At this point, you will have a topsy that has all of the tops in there, with the key being the top, and the value being the number of times it was in there. To get a list of the keys, you say:

Object.keys(topsy);

Object.keys doesn't work in IE. You will end up with topsy =

{
    0: 1,
    30: 1,
    50: 2,
    80: 1,
    100: 2
}

Then you can say

Object.keys(topsy);//[3,30,50,80,100]

You got your analysis and array done at one time.

Upvotes: 2

pimvdb
pimvdb

Reputation: 154838

You can use .map to filter, and .forEach to iterate, although both are not available on older browsers.

var freqs = {},
    tops = divs.map(function(value) {
               return value.top; // map the array by only returning each 'top'
           });

tops.forEach(function(value) {
    // iterate over array, increment freqs of this top
    // or set to 1 if it's not in the object yet
    freqs[value] = freqs[value] + 1 || 1;
});

// tops:  [0, 50, 30, 100, 80, 100, 50]
// freqs: {0: 1, 30: 1, 50: 2, 80: 1, 100: 2}

Upvotes: 1

Joseph Marikle
Joseph Marikle

Reputation: 78530

var divs = [
                {class:'A', top:0,   left:0},
                {class:'B', top:50,  left:60},
                {class:'C', top:30,  left:10},
                {class:'D', top:100, left:180},
                {class:'E', top:80,  left:50},
                {class:'F', top:100,  left:200},
                {class:'G', top:50,  left:80}
    ];

var divsTop = [];

for(var i in divs)
    divsTop.push({"class":divs[i].class,"top":divs[i].top});

You can iterate through and push each (modified) object to another array.

Upvotes: 1

Marshall
Marshall

Reputation: 4766

var divs = [
    {class:'A', top:0,   left:0},
    {class:'B', top:50,  left:60},
    {class:'C', top:30,  left:10},
    {class:'D', top:100, left:180},
    {class:'E', top:80,  left:50},
    {class:'F', top:100,  left:200},
    {class:'G', top:50,  left:80}
];

var tops = [];
for(var i = 0, l = divs.length; i < l; i++) {
    tops.push(divs[i].top);
};

tops; // [ 0, 50, 30, 100, 80, 100, 50 ]

Upvotes: 1

Rob W
Rob W

Reputation: 349032

You have to iterate through the array, and store the top values in a temporary variable. Choosing the variable to be an Array is wise, because an array can easily be looped through.

var test = [];
for(var i=0; i<divs.length; i++){
    test.push(divs[i].top);
}
//test is an array which holds all value of "top"

Upvotes: 1

Related Questions