0

Say I have an object like so...

enter image description here

and I have an array with ids like this..

array = ['21d32fwef23fw32f3', '21we3weasdf23rfwfwf3']

how can I return only the objects that have sys.ids that match the ids in the array

Ive tried using lodash like so..

getWeekItems(weekNumber) {
  this.contenfulService.getWeekItems(weekNumber)
    .then((weekOverview) => {
       this.weekOverviewCompleted = _.filter(weekOverview, this.completedIds);
       console.log(this.weekOverviewCompleted);
      }).then(() => console.log(this.weekOverview));
  }

but Im getting an empty array back??

is there a better way to do this??

2 Answers 2

2

This is how you can get the filtered result :

_.filter(weekOverview, (item) => {
  return this.completedIds.indexOf(item.sys.id) > -1 ;
});

// OR Simple javascript filter function

weekOverview.filter(item => {
  return this.completedIds.indexOf(item.sys.id) > -1 ;    
})
Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks! Do you know how I could do the opposite? so only return the items that don't match those ids??
0

JS function 'filter' does not work on mobile devices. Use the same function defined in Lodash library.

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.