Reputation: 271
I have two arrays of data. One array(allGroups) contains the data of all following and not following groups by user and other array(followingGroups) contains only the list of following groups by the user. Now, I want to compare the allGroups array data id with followingGroups array data id and i need to find, which are all have same id's. I need to list all groups, If a group have same id, i need to show, the user is following that group in the view, else please follow. Please let me know, how can i achieve that. Please check my below array data.
//allGroups array
allGroups = [
{"id": 0, "name": "books", "purpose": "to read"},
{"id": 1, "name": "dress", "purpose": "to wear"},
{"id": 2, "name": "shoe", "purpose": "to walk"},
{"id": 3, "name": "food", "purpose": "to eat"},
{"id": 4, "name": "nature", "purpose": "to view"}
]
//followingGroups array
followingGroups = [
{"following":{"id": 2, "name": "shoe", "purpose": "to walk"}},
{"following":{"id": 3, "name": "food", "purpose": "to eat"}},
{"following":{"id": 4, "name": "nature", "purpose": "to view"}}
]
Upvotes: 0
Views: 1964
Reputation: 327
let allGroups = [
{"id": 0, "name": "books", "purpose": "to read"},
{"id": 1, "name": "dress", "purpose": "to wear"},
{"id": 2, "name": "shoe", "purpose": "to walk"},
{"id": 3, "name": "food", "purpose": "to eat"},
{"id": 4, "name": "nature", "purpose": "to view"}
];
let followingGroups = [
{"following":{"id": 2, "name": "shoe", "purpose": "to walk"}},
{"following":{"id": 3, "name": "food", "purpose": "to eat"}},
{"following":{"id": 4, "name": "nature", "purpose": "to view"}}
];
let followingIds = followingGroups.map(group => group.following.id);
let allGroupsUserSpecific = allGroups.map(group => (
{...group, following: followingIds.includes(group.id)})
);
This code creates a new array of allGroups
called allGroupsUserSpecific
which contains a flag to tell the frontend whether the user is already following that group. If you log the allGroupsUserSpecific
var, this is the output...
{id: 0, name: "books", purpose: "to read", following: false}
{id: 1, name: "dress", purpose: "to wear", following: false}
{id: 2, name: "shoe", purpose: "to walk", following: true}
{id: 3, name: "food", purpose: "to eat", following: true}
{id: 4, name: "nature", purpose: "to view", following: true}
Upvotes: 1
Reputation: 711
It is quite simple, just select the field that you want to compare, you should also need to read this doc array loop
const allGroups = [
{"id": 0, "name": "books", "purpose": "to read"},
{"id": 1, "name": "dress", "purpose": "to wear"},
];
const userGroups = [
{"following":{"id": 0, "name": "shoe", "purpose": "to walk"}},
{"following":{"id": 3, "name": "food", "purpose": "to eat"}},
{"following":{"id": 4, "name": "nature", "purpose": "to view"}}
];
// Collect ids of allGroups
const ids = allGroups.map(group => group.id);
const sameGroup = userGroups.map(group => {
const { following } = group;
if (ids.includes(following.id)) {
console.log(JSON.stringify(group))
return group; // return group with id: 0
}
});
Upvotes: 2