I need to filter an JS object as below format. I have some array of Object. every object contain title and data. I need to filter the object. title will not repet in object it will make an array of data and store every data in that. Object
let results = [
{
"title": "exam",
"data": {
"status": "Active",
"studentId": "44",
"universityId": "0",
"mediaId": "12",
"mediaName": "massey university",
"mediaSrc": "https://unisearch-static-contents.s3.ap-southeast-1.amazonaws.com/ADMIN/massey%20university-9a2b9a22-b648-4ef1-a806-97bb0da9b337.jpg",
"mediaDownload": 4
}
},
{
"title": "prospectus",
"data": {
"status": "Active",
"studentId": "44",
"universityId": "0",
"mediaId": "12",
"mediaName": "massey university",
"mediaSrc": "https://unisearch-static-contents.s3.ap-southeast-1.amazonaws.com/ADMIN/massey%20university-9a2b9a22-b648-4ef1-a806-97bb0da9b337.jpg",
"mediaDownload": 4
}
},
{
"title": "prospectus",
"data": {
"status": "Active",
"studentId": "44",
"universityId": "23",
"mediaId": "12",
"mediaName": "massey university",
"mediaSrc": "https://unisearch-static-contents.s3.ap-southeast-1.amazonaws.com/ADMIN/massey%20university-9a2b9a22-b648-4ef1-a806-97bb0da9b337.jpg",
"mediaDownload": 4
}
},]
Filter object will be like this. title will not be repeated it will make an array.
"results": [
{
"title": "exam",
"data": {
"status": "Active",
"studentId": "44",
"universityId": "0",
"mediaId": "12",
"mediaName": "massey university",
"mediaSrc": "https://unisearch-static-contents.s3.ap-southeast-1.amazonaws.com/ADMIN/massey%20university-9a2b9a22-b648-4ef1-a806-97bb0da9b337.jpg",
"mediaDownload": 4
}
},
{
"title": "prospectus",
"data": [{
"status": "Active",
"studentId": "44",
"universityId": "0",
"mediaId": "12",
"mediaName": "massey university",
"mediaSrc": "https://unisearch-static-contents.s3.ap-southeast-1.amazonaws.com/ADMIN/massey%20university-9a2b9a22-b648-4ef1-a806-97bb0da9b337.jpg",
"mediaDownload": 4
},
{
"status": "Active",
"studentId": "44",
"universityId": "23",
"mediaId": "12",
"mediaName": "massey university",
"mediaSrc": "https://unisearch-static-contents.s3.ap-southeast-1.amazonaws.com/ADMIN/massey%20university-9a2b9a22-b648-4ef1-a806-97bb0da9b337.jpg",
"mediaDownload": 4
}]
I have tried this.
let filterData = [];
mainData.data.results.map((value) => {
if (filterData.length == 0) {
let obj = {
title: "title",
data: [{ ...value.data }],
};
filterData.push(obj);
} else {
let found = false;
}
});
datato be either an object or an array? I would recommend using an array for all of them, even if for only one entry just so you've got a consistent API