0

I have an object that contains these data.

 {id: 1864,
 courseContentId: 481,
 fileName: GymMembership.jpg
 }
 {id: 1865,
 courseContentId: 481,
 fileName: Field.jpg
 }
 {id: 1866,
 courseContentId: 482,
 fileName: Track.jpg
 }

And i wanted to show them seperately on the view based on the courseContentId by creating a new object and this is my desired output.

 {id: 1864,
 courseContentId: 481,
 fileName: GymMembership.jpg
 }
 {id: 1865,
 courseContentId: 481,
 fileName: Field.jpg
 }

 {id: 1866,
 courseContentId: 482,
 fileName: Track.jpg
 }

What kind of javascript function is suitable for this?

The Ninja
  • 45
  • 1
  • 7
  • 1
    Possible duplicate of [Javascript: How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – slider Nov 28 '18 at 01:40
  • You answered your question in the title.... [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)! – Nick Nov 28 '18 at 01:40
  • @slider Yes but in my case i need to show them seperately based on the courseContentId. It's similar to the show only unique but i need to show all with the same courseContentId seperately. – The Ninja Nov 28 '18 at 01:48

3 Answers3

1
let yourDesiredContentId = 481;
let result = array.filter(el => el.courseContentId === yourDesiredContentId);
Stephen.W
  • 1,649
  • 13
  • 14
1

Just use _.GroupBy, rather than applying multiple filters.

courses = [{id: 1864,
 courseContentId: 481,
 fileName: 'GymMembership.jpg'
 },
 {id: 1865,
 courseContentId: 481,
 fileName: 'Field.jpg'
 },
 {id: 1866,
 courseContentId: 482,
 fileName: 'Track.jpg'
 }]
 
var grouped = _.groupBy(courses, function(course) {
  return course.courseContentId;
});
console.log("Grouped")
console.log(grouped);
console.log("Filter by 481")
console.log(grouped["481"]);
console.log("Filter by 482")
console.log(grouped["482"]);
<script src="https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js"></script>
Arjun Kava
  • 5,303
  • 3
  • 20
  • 20
  • Thanks. Almost but i'm not supposed to put manually the ID. What function in javascript that gets all the similar property. – The Ninja Nov 28 '18 at 07:33
  • Correct me if I am wrong, You want grouped array of similar `courseContentId`. am I right? if yes then I have updated answer for same. You can find first consoled output, otherwise let me know. – Arjun Kava Nov 28 '18 at 10:02
  • Thanks. But i have one more question. How can i keep the original index number after using group by? – The Ninja Nov 29 '18 at 03:22
  • @TheNinja, You can append index explicitly in array to sort the same or you can sort by "key" of grouped array. Let me what is reason behind getting same index. – Arjun Kava Nov 29 '18 at 14:06
0

Not really sure if there's an easy way to do that. Anyway, try this:

var test = [{
    id: 1864,
    courseContentId: 481,
    fileName: 'GymMembership.jpg'
  },
  {
    id: 1865,
    courseContentId: 481,
    fileName: 'Field.jpg'
  },
  {
    id: 1866,
    courseContentId: 482,
    fileName: 'Track.jpg'
  }
];

console.log(filter(test, 'courseContentId', 481));

function filter(arr, key, value) {
  return arr.reduce((data, item) => {
    if (item[key] == value) data.push(item);
    return data;
  }, []);
}
ACD
  • 1,431
  • 1
  • 8
  • 24