1

I'm trying to compare two JSONs files in NodeJs. If there is a review that matches the place id, I need to push the review data into the places JSON. If there are no matching reviews, it pushes an empty array.

Here is the code:

//places JSON

[{
        "id": 1,
        "title": "Hotel in Sidney",
        "maxNumberOfGuests": 5,
        "description": "Quiet place by the water.",
        "createdAt": "2019/12/7 14:34",
        "price": 120
    },
    {
        "id": 2,
        "title": "Cabin in Italy",
        "maxNumberOfGuests": 2,
        "description": "Romantic lake cabin for two.",
        "createdAt": "2019/4/7 10:00",
        "price": 250
    }
]

//reviews JSON

[{
        "id": 1,
        "numberOfStars": 3,
        "content": "Comfy place",
        "placeId": 1,
        "createdAt": 12345
    },
    {
        "id": 2,
        "numberOfStars": 4,
        "content": "Awesome lake view.",
        "placeId": "",
        "createdAt": 23456
    }
]

Here is the desired result:

[{
        "id": 1,
        "title": "Hotel in Sidney",
        "maxNumberOfGuests": 5,
        "description": "Quiet place by the water.",
        "createdAt": "2019/12/7 14:34",
        "reviews": [{
            "id": 1,
            "numberOfStars": 3,
            "content": "Comfy place",
            "placeId": 1,
            "createdAt": 12345
        }],
        "price": 120
    },
    {
        "id": 2,
        "title": "Cabin in Italy",
        "maxNumberOfGuests": 2,
        "description": "Romantic lake cabin for two.",
        "createdAt": "2019/4/7 10:00",
        "reviews": [],
        "price": 250
    }
]

this is how far I could get:

places.forEach(p => {
  const { id } = p;
  console.log(id);
  return id;
});

reviews.forEach(r => {
  const { id, numberOfStars, content, placeId, createdAt } = r;
  // console.log(id, numberOfStars, content, placeId, createdAt);
  console.log(r);
  return r;
});

//node express routes to places where will display the desired result. 

router.get('/places', function(req, res) {
  res.json(places);
});

I just can't make it work and need some help.

Thanks in advance.

2 Answers 2

1

try this

let places =[{
        "id": 1,
        "title": "Hotel in Sidney",
        "maxNumberOfGuests": 5,
        "description": "Quiet place by the water.",
        "createdAt": "2019/12/7 14:34",
        "price": 120
    },
    {
        "id": 2,
        "title": "Cabin in Italy",
        "maxNumberOfGuests": 2,
        "description": "Romantic lake cabin for two.",
        "createdAt": "2019/4/7 10:00",
        "price": 250
    }
];

let reviews =[{
        "id": 1,
        "numberOfStars": 3,
        "content": "Comfy place",
        "placeId": 1,
        "createdAt": 12345
    },
    {
        "id": 2,
        "numberOfStars": 4,
        "content": "Awesome lake view.",
        "placeId": "",
        "createdAt": 23456
    }
];
places.forEach(function(place) {
  place.reviews = reviews.filter(review => review.placeId ===place.id);
});


console.log(places);

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

Comments

0

I would first reduce the reviews array into an object keyed by placeId.

const reviews = [{
    "id": 1,
    "numberOfStars": 3,
    "content": "Comfy place",
    "placeId": 1,
    "createdAt": 12345
  },
  {
    "id": 2,
    "numberOfStars": 4,
    "content": "Awesome lake view.",
    "placeId": "",
    "createdAt": 23456
  }
];

const reviewsHashmap = reviews.reduce((acc, review) => {
  if (!review.placeId) return acc;
  acc[review.placeId] = acc[review.placeId] || [];
  acc[review.placeId].push(review);
  return acc;
}, {})

This makes it more performant when adding the reviews property, because now you do not need to filter the reviews array again and again for each place.

const places = [
  {
    "id": 1,
    "title": "Hotel in Sidney",
    "maxNumberOfGuests": 5,
    "description": "Quiet place by the water.",
    "createdAt": "2019/12/7 14:34",
    "price": 120
  },
  {
    "id": 2,
    "title": "Cabin in Italy",
    "maxNumberOfGuests": 2,
    "description": "Romantic lake cabin for two.",
    "createdAt": "2019/4/7 10:00",
    "price": 250
  }
];

const placesWithReviews = places.map(place => ({
  ...place,
  reviews: reviewsHashmap[place.id] || []
}))

Now you should have the original places array, but with each place an additional reviews property.

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.