I am trying to merge or basically perform an inner join on two datasets provided by https://jsonplaceholder.typicode.com/comments
and https://jsonplaceholder.typicode.com/posts
. The join would of course be using the PostId
and id
keys of both data sets respectively; however I am unsure of how to actually do this. I've added my codepen: https://codepen.io/gabedesigns/pen/qMGgxY?editors=1111 and I seem to be getting an error saying that I am missing '()' for my constructor. After figuring this out I will also need to search for posts with provided post ID and the comments corresponding the post. Perhaps I need nested for loops in order to make this work? I've tried to look up some other information on this and couldn't seem to find much. If there are any other resources I may have skipped over please don't be afraid to link it below. I saw a post on here already talking about "inner joins" with javascript instead of SQL, but this is where I ran into some problems. Here is some code:
function getResults() {
const equijoin = (xs, ys, primary, foreign, sel) => {
const ix = xs.reduce((ix, row) => ix.set(row[primary], row), new Map());
return ys.map(row => sel(ix.get(row[foreign]), row));
};
const postData = fetch("https://jsonplaceholder.typicode.com/posts")
.then(function(response) {
return response.json();
});
const commentData = fetch("https://jsonplaceholder.typicode.com/comments")
.then(function(response) {
return response.json();
});
const result = equijoin(postData, commentData,"id", "postId", ({title,
body}, {name,body}) => ({title,body,name}));
}