My apologies if this question has been asked many times before. I've searched Stackoverflow thoroughly and found nothing. It may be I was using the wrong search terms or didn't understand how the solutions given applied to my question.
I have two Javascript objects; pageviews
and enrollments
. Both contain data retrieved from a learning management system in separate API calls. Both objects contain a user_id
attribute and some records in each object will have the same user_id
values. How would I retrieve the value of enrollments.name when the values of pageviews.user_id
and enrollments.user_id
match?
var pageviews = [{
id: 1,
page: "Home",
user_id: "100032",
},
{
id: 2,
page: "Assignment 1",
user_id: "123032",
},
{
id: 3,
page: "Discussion 2",
user_id: "147032",
},
];
var enrollments = [{
id: 1,
name: "Amy",
role: "Student",
user_id: "100032",
},
{
id: 2,
name: "Bill",
role: "Teacher",
user_id: "123032",
},
{
id: 3,
name: "Carol",
role: "Student",
user_id: "147032",
},
];
for (var id in pageviews) {
var thisUser = pageviews[id].user_id;
console.log("To match - " + thisUser);
for (var id in enrollments) {
if (enrollments[id].useer_id == thisUser) {
console.log("Returned - " + enrollments[id].nane);
}
}
}
I think you're looking for something like this:
enrollments.forEach(enrollment => {
const pageviewinfo = pageviews.find(pageview => pageview.user_id === enrollment.user_id);
console.log(enrollment.name, pageviewinfo);
});
It loops through the enrollments and finds the pageview for a given user_id.
Good luck!
const pageviews = [{ id: 1, page: "Home", user_id: "100032", }, { id: 2, page: "Assignment 1", user_id: "123032", }, { id: 3, page: "Discussion 2", user_id: "147032", }, ]; const enrollments = [{ id: 1, name: "Amy", role: "Student", user_id: "100032", }, { id: 2, name: "Bill", role: "Teacher", user_id: "123032", }, { id: 3, name: "Carol", role: "Student", user_id: "147032", }, ]; enrollments.forEach(enrollment => { const pageviewinfo = pageviews.find(pageview => pageview.user_id === enrollment.user_id); console.log(enrollment.name, pageviewinfo); });
So from what I understand, you are trying to get the names of enrollments that have matching user id to each pageview user_id. If that's what you are trying to achieve, then the below snippet should do it. Goodluck
var pageviews = [{ id: 1, page: "Home", user_id: "100032", }, { id: 2, page: "Assignment 1", user_id: "123032", }, { id: 3, page: "Discussion 2", user_id: "147032", }, ]; var enrollments = [{ id: 1, name: "Amy", role: "Student", user_id: "100032", }, { id: 2, name: "Bill", role: "Teacher", user_id: "123032", }, { id: 3, name: "Carol", role: "Student", user_id: "147032", }, ]; // loop through the page views pageviews.forEach(pageview => { // then get the names of enrollements that matches the current pageview user_id let enrollmentsNames = enrollments.filter(data => data.user_id === pageview.user_id).map(data => data.name); // if the result is not empty, then log the names of enrollments that has matching user id with the current pageview user_id if (enrollmentsNames.length) console.log(enrollmentsNames) // if it's certain that a pageview won't have more than 1 enrollment then just go ahead and log the first element if (enrollmentsNames.length) console.log(enrollmentsNames[0]) });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.