-1

I have this long list of array and I want to filter the object return by id. For example, I want to get objects with the same id, in this case object at index 0 and index 2. How can i achieve this? I have tried the for loop method but it's not going anywhere

var arr = [
  {
    "name": "David",
    "last_updated": "2021-04-12 15:42:51",
    "id": "175",
    "class": "CLASS 2019",
    "stops": [
      {
        "total": "29",
        "graduated": "1900"
      },
    ],
  },
    {
    "name": "Cameron",
    "last_updated": "2021-04-12 15:42:51",
    "id": "180",
    "class": "CLASS 2021",
    "stops": [
      {
        "total": "40",
        "graduated": "2500"
      },
    ],
  },
  {
    "name": "Rose",
    "last_updated": "2021-04-12 15:42:51",
    "id": "175",
    "class": "CLASS 2008",
    "stops": [
      {
        "total": "50",
        "graduated": "1000"
      },
    ],
  },

This is a short snippet that I have in mind and tried. I'm aware that it doesn't make sense hence why I'm asking here. Any explanations and workarounds is very much appreciated

for(let i=0; i<arr.length; i++) {
   if(arr[i].id === arr[i].id) {
      console.log(arr[i])
   }
}
1
  • @programmer211216 please take a look at my answer when you have a chance. I rewrite exactly what you are trying to achieve using array methods, which highly simplifies the logic. Commented Apr 29, 2021 at 23:36

2 Answers 2

1

Please correct me if I am misunderstanding here, but you simply want to filter the array of objects to only keep objects whose id value appears more than once in the array.

If that's the case, then my solution below should answer your question. Essentially, what it does is filter the source array by a map of all id values and filters to only objects whose id appears more than once. Using length - 1 works interchangeably with length > 1 here as subtracting 1 will product a falsy value 0 for those with only one instance of their id. The only difference here would be that this would not filter objects without an id property.

If you will be dealing with objects without an id property and would like to exclude those in the final result, change length - 1 to length > 1.

const arr = [
  { name: "David", last_updated: "2021-04-12 15:42:51", id: "175", class: "CLASS 2019", stops: [ { total: "29", graduated: "1900" } ] },
  { name: "Cameron", last_updated: "2021-04-12 15:42:51", id: "180", class: "CLASS 2021", stops: [ { total: "40", graduated: "2500" } ] },
  { name: "Rose", last_updated: "2021-04-12 15:42:51", id: "175", class: "CLASS 2008", stops: [ { total: "50", graduated: "1000" } ] }
];

const uniqObjs = [];
const dupeObjs = [];

arr.forEach(obj => [uniqObjs,dupeObjs][+(arr.map(obj => obj.id).filter(id => id === obj.id).length > 1)].push(obj));

console.log('uniqObjs:',uniqObjs);
console.log('dupeObjs:',dupeObjs);

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, this is correct. I have tried your solution and it works! Only thing now is that I have loop through and got my desired data, let's say class. How can i get the remaining one class whose id is unique from the rest?
@programmer211216 are you saying you would like to have a separate function that returns only objects with unique id values? We can probably achieve this as simply as we did before. I'll adjust my answer.
I want to populate values with both unique and non-uniques. I have already got the values for non-unique id's thanks to your solution, but I also want the values from non-unique id. Like concatenate those values...
@programmer211216 I've adjusted my answer to populate both. There were a couple of quick ways to do this. The simplest would have been a ternary between to different push() methods to either the unique or duplicate array, but I opted to consolidate the logic one step further. Instead, I push to either array by interpreting what was before a boolean filter result, now as an integer 0 or 1 and use that value to determine which array I push to. This way, everything takes place in one fluid line.
@programmer211216 If you would prefer for the final result to all be consolidated into one array with the duplicate values first, we can do that rather easily, or if you'd prefer the duplicate and unique values exist as two nested arrays within the top-level array, that's not too difficult either.
-2

You can use array.filter

var arr = [{
    "name": "David",
    "last_updated": "2021-04-12 15:42:51",
    "id": "175",
    "class": "CLASS 2019",
    "stops": [{
      "total": "29",
      "graduated": "1900"
    }, ],
  },
  {
    "name": "Cameron",
    "last_updated": "2021-04-12 15:42:51",
    "id": "180",
    "class": "CLASS 2021",
    "stops": [{
      "total": "40",
      "graduated": "2500"
    }, ],
  },
  {
    "name": "Rose",
    "last_updated": "2021-04-12 15:42:51",
    "id": "175",
    "class": "CLASS 2008",
    "stops": [{
      "total": "50",
      "graduated": "1000"
    }, ],
  },
]

const id175 = arr.filter(item => item.id === '175');
console.log(id175)

2 Comments

This assumes you know the ID to look for rather than just pulling duplicates out without knowing an ID
I have this long list of array and I want to filter the object return by id.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.