How to cut an array from an array of object literals?

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

My idea is to sort the array and cut 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 country property is "United States". This is my crazy idea that 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(); 
+3
javascript sorting arrays slice object-literal
May 31 '12 at 5:27
source share
6 answers
 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/

+1
May 31 '12 at 5:41
source share
— -

There is an easier way to do this, I think this is what you want var new_arr = arr.filter(function(obj){ return obj['country'] === 'United States'; }) This will filter your results in new_arr Of course you can make it better and more general than just the "United States"

Edit: Oops, you have a question right now

Answer: Nested filters :)

 function getKeyStuff(key) { return arr.filter( function(obj) { var new_arr = arr.filter( function(inner_obj) { return inner_obj[key] === obj[key]; }); return new_arr.length > 1; }); } 
+2
May 31 '12 at 5:34 a.m.
source share

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

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

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

Please note that the .filter() method is not available in IE prior to version 9, but the MDN page I linked to above shows you how to implement it, so reading this page should itself answer your question.

Note also that in the (non-working) code in the question, your two for loops basically do the same thing as each other, because they iterate over arr , so it doesn't make sense to nest them. You should not use a for..in loop for an array, but if the key values ​​are numerical indexes, they will not be able to somehow get the properties of the object stored in each index.

EDIT:

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

OK, after re-reading this, I think you really didn’t want to select items by specifying a country, would you like to select items for any country that had duplicate entries? So, if there were three more elements that all had New Zealand, would you like to choose them in addition to the United States? If so, you can 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; }); 
+2
May 31 '12 at 5:35
source share

Here is my solution:

 // assuming arr is already set var num_obj = arr.length; var obj_by_country = {}, obj; for (var i = 0; i < num_obj; i++) { obj = arr[i]; if (!obj_by_country[obj.country]) { obj_by_country[obj.country] = []; } obj_by_country[obj.country].push(obj); } // build final array var final_array = []; for (i in obj_by_country) { if (obj_by_country[i].length > 1) { final_array.push(obj_by_country[i]); } } 
+1
May 31 '12 at 5:34 a.m.
source share

Can you use jQuery?

 var arr = []; arr[0] = { country: "United States", num: 27 }; arr[1] = { country: "Australia", num: 5 }; arr[2] = { country: "United States", num: 7 }; var newArray = []; $.each(arr, function(){ if(this.country == "United States") newArray.push(this); }); 
+1
May 31 '12 at 5:38
source share
 getByKey = function(arr, key, value) { var results = []; for(var i = 0; i < arr.length; i++) { if(arr[i][key] === value) { results.push(arr[i]); } } return results } 

here's a working example http://jsfiddle.net/michaelghayes/UyHYz/2/

0
May 31 '12 at 5:34
source share



All Articles