javascript - Filtering an array of objects using underscor functions JS -
i have array of objects looks like:
object{ name: array[3] [0]: firstname: john lastname : smtih classid : 123 [1]: firstname: jane lastname : doe id : 321 [2]: firstname: john lastname : smtih classid : 456 }
i need filter out array based on uniqueness, no person same firstname , last name , if there 2 of same people, filter same people classid have chosen.
for example, if there 2 john smith in list, , if current classid 123 end result should like
object{ name: array[2] 0: firstname: john lastname : smtih classid : 123 1: firstname: jane lastname : doe id : 321 }
so basically, need check array of objects make sure 2 people dont have same first name , last name , if need filter people classid.
how can filter underscore functions?
thanks
try:
var obj = { name: [ { firstname: "john", lastname : "smtih", classid : 123 }, { firstname: "jane", lastname : "doe", classid : 321 }, { firstname: "john", lastname : "smtih", classid : 321 } ] }; obj.name = _.uniq(obj.name, function(item){ return [item.firstname, item.lastname].join() }); document.write("<pre>" + json.stringify(obj, null, 4) + "</pre>")
<script src="http://underscorejs.org/underscore.js"></script>
_.uniq simple way so, specifying unique string "firstname, lastname".
Comments
Post a Comment