0

I have these nested objects. How can I check if the group_id are all the same?

console.log(getData);

{…}
    "$": function g()​
    0: {…}
        DT_RowId: "row_1"
        groups: Object { group_id: 2, … }
    1: {…}
        DT_RowId: "row_2"
        groups: Object { group_id: 2, … }
    2: {…}
        DT_RowId: "row_3"
        groups: Object { group_id: 2, … }
    3: {…}
        DT_RowId: "row_4"
        groups: Object { group_id: 2, … }           

A .each() provides me: ​​

$.each( getData, function ( id, val ) {
    console.log(val.groups.group_id);
});

2
2
2
2
2
  • If getData is an array, you can use Array.every() to return true/false if every item in an array meets a given condition. Commented Feb 6, 2020 at 21:19
  • What is getData? Seems like it is a jQuery object?? Hard to answer without knowing what it is. Commented Feb 6, 2020 at 21:48

4 Answers 4

1

Unclear what the data exactly is. If it is in array format every() is what you need, compare it to the first index value

var objData = [
  { groups: { group_id: 2 } },
  { groups: { group_id: 2 } },
  { groups: { group_id: 2 } },
  { groups: { group_id: 2 } },
  { groups: { group_id: 2 } },
]

var allMatch = objData.every((n, i, a) => n.groups.group_id===a[0].groups.group_id)

console.log(allMatch)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

It seems like it might be an object in that case simple conversion can make it into an array

var objData = {
  '$': {},
  0: { groups: { group_id: 2 } },
  1: { groups: { group_id: 2 } },
  2: { groups: { group_id: 2 } },
  3: { groups: { group_id: 2 } },
  4: { groups: { group_id: 2 } },
}

var allMatch = Object.values(objData)
  .filter(o => o.groups)
  .every((n, i, a) =>
    n.groups.group_id === a[0].groups.group_id
  )

console.log(allMatch)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

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

Comments

0

In the case of that being nested objects you could do something like: Object.values(getData).map(val => val.groupId).every((groupId, i, arr) => groupId === arr[0]).

Comments

0

if getData is an object

  Object.keys(getData).every(key => getData[key].group_id === getData[0].group_id)

We first get the keys of the getData object container, and then we use the array of keys to access the getData object to check each obj.group_id

if getData is an array containing objects

  getData.every(obj => getData[obj].group_id === getData[0].group_id)

Similar to the above, except we can directly iterate over the array of objects

3 Comments

Thanks for the help. I don't know if group_id equals 2 ... I need to check if the are all the same ...
This doesn't guarantee that all the items have the same group id, this ensures that all items have the id of 2
Ah I misread the question, apologies. @PhilippM do you know what ID you'd like to check for? I've updated it to just make sure that it matches the first group's group_id
0

You can use Object.keys to iterate though the object and use every to test all values satisfy the condition.

const nestedObj = {
    0: {
        DT_RowId: "row_1",
        groups: { group_id: 2}
    },
    1: {
        DT_RowId: "row_2",
        groups: { group_id: 2}
    },
    2: {
        DT_RowId: "row_3",
        groups: { group_id: 2}
    },
    3: {
        DT_RowId: "row_4",
        groups: { group_id: 2}
    }    

}

const GROUP_ID = nestedObj[0].groups.group_id;
const isGroupIdSame = Object.keys(nestedObj).every(key => nestedObj[key].groups.group_id === GROUP_ID);

console.log(isGroupIdSame)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.