Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I slice an array from an array of object literals?

I have this array, in which each index contains an object literal. All of the object literals have the same properties. Some of the object literals have the same value for a given property, and I want to create a new array containing only those object literals.

My idea is to sort the array, and slice it into a new array...

Here is the array:

var arr = [];

arr[0] =
{
    country: "United States",
    num: 27
};

arr[1] =
{
    country: "Australia",
    num: 5
};

arr[2] =
{
    country: "United States",
    num: 7
};


So, I want to create a new array containing only those objects where the property country is "United States". This is my crazy idea so far, which doesn't work:

function getNewArray(arr)
{
    var arr2 = [];

    for(var key in arr)
    {
        for(var i = 0; i < arr.length - 1; i++)
        {
            if(arr.hasOwnProperty(key) && arr[i].name == arr[i + 1].name)
            {
                    arr2[i] = arr.slice(key);
            }
        }
    }

    return arr2;
}

var arr3 = getNewArray(arr).sort();
like image 909
Ian Campbell Avatar asked May 31 '12 05:05

Ian Campbell


2 Answers

"I want to create a new array containing only those objects where the property country is "United States""

This is exactly what the Array.filter() method is for:

var filteredArray = arr.filter(function(val, i, a) {
                                  return val.country==="United States";
                    });

Note that the .filter() method isn't available in IE before version 9, but the MDN page I linked to above shows you exactly how to implement it so reading that page should in itself answer your question.

Note also that in the (non-working) code in the question, your two for loops are basically doing the same thing as each other because they're both iterating over arr, so it doesn't make sense to nest them like that. You shouldn't use a for..in loop on an array, but if you do the key values will be the numeric indexes, it doesn't somehow pick up the properties of the object stored at each index.

EDIT:

"Some of the object literals have the same value for a given property, and I want to create a new array containing only those object literals."

OK, re-reading this I guess you didn't really want to select elements by specifying a country, you wanted to select elements for any country that had duplicate entries? So if there were another three elements that all had "New Zealand" you'd want to select them in addition to the "United States" ones? If so, you could do something like this:

var countryCount = {},
    i;
for (i = 0; i < arr.length; i++)
    if (countryCount.hasOwnProperty(arr[i].country)
       countryCount[arr[i].country]++;
    else
       countryCount[arr[i].country] = 1;

var filteredArr = arr.filter(function(val, i, a) {
                      return countryCount[val.country] > 1;
                  });
like image 154
nnnnnn Avatar answered Nov 14 '22 23:11

nnnnnn


var getCountry = function (country) {
    var out = [];
    for (var i = 0, len = arr.length; i < len; i++)
        if (arr[i].country === country) out.push(arr[i]);
    return out;
};

Demo: http://jsfiddle.net/YwytD/1/

like image 29
elclanrs Avatar answered Nov 15 '22 00:11

elclanrs