2 Replies Latest reply on Nov 1, 2012 10:38 PM by Mark Nongkhlaw

    How do I get the unique elements from an array of hashes

    Mark Nongkhlaw

      This is my array which I get from a JSON webservice :

       

      [

      {

      imp_code: 455,

      remarks: "",

      obj_code: "35",

      project_id: 821

      },

      {

      imp_code: 890,

      remarks: "",

      obj_code: "31",

      project_id: 1499

      },

      {

      imp_code: 646,

      remarks: "",

      obj_code: "35",

      project_id: 821

      },

      {

      imp_code: 377,

      remarks: "",

      obj_code: "31",

      project_id: 730

      },

      {

      imp_code: 646,

      remarks: "",

      obj_code: "31",

      project_id: 820

      },

      {

      imp_code: 646,

      remarks: "",

      obj_code: "31",

      project_id: 821

      },

      {

      imp_code: 646,

      remarks: "",

      obj_code: "35",

      project_id: 821

      },

       

       

      <snip>

      ]

       

      I need to extract all the elements having UNIQUE imp_code value for a particular project_id value. So, lets say my project_id value is 821. From above example I need to get the array below :

       

      [

      {

      imp_code: 646,

      remarks: "",

      obj_code: "35",

      project_id: 821

      },

      {

      imp_code: 455,

      remarks: "",

      obj_code: "35",

      project_id: 821

      }

      ]

       

      The order of the elements doesn't matter.  I'd be most obliged if anyone can help me achieve this using Ruby code.