Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
angular js foreach check duplicate values before push into the array

I am using below array for my foreach function

  $scope.categoryList =  [
    {
        "id": 44,
        "creationTimestamp": "2019-11-15 17:11:17",
        "name": "FIXED ",
        "description": "FIXED",
        "$$hashKey": "object:108"
    },
    {
        "id": 44,
        "creationTimestamp": "2019-11-15 17:09:32",
        "name": "SAV",
        "description": "SAV",
        "$$hashKey": "object:109"
    },
    {
        "id": 76,
        "creationTimestamp": "2021-08-17 14:19:14",
        "name": "TEST CAT",
        "description": "TEST CAT",
        "$$hashKey": "object:110"
    },
    {
        "id": 77,
        "creationTimestamp": "2021-08-17 14:19:14",
        "name": "TEST CAT",
        "description": "TEST CAT",
        "$$hashKey": "object:110"
    } 
 
]

I need to get ids from the above array. I need to remove duplicate values before the push method.

this is my code

   angular.forEach($scope.categoryList, function (object) {
                    $scope.myNewarray.push(object.id);
                });

my new array data should be like as below

$scope.myNewarray = [44,76,77]

I need to get unique ids only for the my new array.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use Set to get the unique value from the array

$scope.categoryList = [
  {
    id: 44,
    creationTimestamp: "2019-11-15 17:11:17",
    name: "FIXED ",
    description: "FIXED",
    $$hashKey: "object:108",
  },
  {
    id: 44,
    creationTimestamp: "2019-11-15 17:09:32",
    name: "SAV",
    description: "SAV",
    $$hashKey: "object:109",
  },
  {
    id: 76,
    creationTimestamp: "2021-08-17 14:19:14",
    name: "TEST CAT",
    description: "TEST CAT",
    $$hashKey: "object:110",
  },
  {
    id: 77,
    creationTimestamp: "2021-08-17 14:19:14",
    name: "TEST CAT",
    description: "TEST CAT",
    $$hashKey: "object:110",
  },
];

$scope.myNewarray = [...new Set($scope.categoryList.map((o) => o.id))];
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!