0

I am having a JSON array:

{
        "assets": [
          {
            "extension": "png",
            "id": "3",
            "fileType": "IMG",
            "name": "Screenshot-2020-07-09-at-4-25-30-PM_XX"
          },
          {
            "extension": "jpg",
            "id": "10",
            "fileType": "IMG",
            "name": "vertical-1_XXX"
          },
          {
            "extension": "mp4",
            "id": "15",
            "fileType": "VID",
            "name": "aaa"
          },
          {
            "extension": "pdf",
            "id": "19",
            "fileType": "DOC",
            "name": "ccc"
          }
        ]
      }

I need to group by filetype say for example for filetype IMG only 2

[
              {
                "extension": "png",
                "id": "3",
                "fileType": "IMG",
                "name": "Screenshot-2020-07-09-at-4-25-30-PM_XX"
              },
              {
                "extension": "jpg",
                "id": "10",
                "fileType": "IMG",
                "name": "vertical-1_XXX"
              }]

likewise, for the other 2 filetypes, the length of the array will be 1, since it has each element in the array

Please help me to groupby fileType in react

9
  • How about sorting the array of objects on type? Commented Jul 10, 2020 at 3:29
  • can you share code @HarmandeepSinghKalsi Commented Jul 10, 2020 at 3:29
  • assets.sort((a,b) => a.fileType .localeCompare(b.fileType)). I am assuming assets as an array in the input Commented Jul 10, 2020 at 3:31
  • can you help me with groupby Commented Jul 10, 2020 at 3:34
  • Doesn't this suffice your need here, it is also grouping only? Have a look at this , if you want something like this stackoverflow.com/questions/40774697/… Commented Jul 10, 2020 at 3:35

3 Answers 3

1

Hope this is what You are looking for :

var testObj = {
        "assets": [
          {
            "extension": "png",
            "id": "3",
            "fileType": "IMG",
            "name": "Screenshot-2020-07-09-at-4-25-30-PM_XX"
          },
          {
            "extension": "jpg",
            "id": "10",
            "fileType": "IMG",
            "name": "vertical-1_XXX"
          },
          {
            "extension": "mp4",
            "id": "15",
            "fileType": "VID",
            "name": "aaa"
          },
          {
            "extension": "pdf",
            "id": "19",
            "fileType": "DOC",
            "name": "ccc"
          }
        ]
      }
      
      const groupBy = function(toFilter,type){
         let res = testObj.assets.filter(obj=>obj[toFilter]===type);
         return res;
      }
      
      console.log(groupBy('fileType',"IMG"));
      
      //LIKE WISE PASS ON THE toFilter , and type 

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

Comments

1

I guess this is what you want to do. Use .reduce to group the elements based on their filetype.

var arr = {
        "assets": [
          {
            "extension": "png",
            "id": "3",
            "fileType": "IMG",
            "name": "Screenshot-2020-07-09-at-4-25-30-PM_XX"
          },
          {
            "extension": "jpg",
            "id": "10",
            "fileType": "IMG",
            "name": "vertical-1_XXX"
          },
          {
            "extension": "mp4",
            "id": "15",
            "fileType": "VID",
            "name": "aaa"
          },
          {
            "extension": "pdf",
            "id": "19",
            "fileType": "DOC",
            "name": "ccc"
          }
        ]
      };
      
      
var data = arr.assets;

var r= data.reduce((acc, ele)=>{
  acc[ele.fileType] = acc[ele.fileType] ? [...acc[ele.fileType], ele] : [ele];
  return acc
},{});

console.log(r['IMG'])

EDIT: To get only IMG grouping you can just do r['IMG']

1 Comment

can you explicity add code to retrieve filetype with IMG only please
1

Working Code !!

using map() method in ES6

const data = {
  "assets": [{
      "extension": "png",
      "id": "3",
      "fileType": "IMG",
      "name": "Screenshot-2020-07-09-at-4-25-30-PM_XX"
    },
    {
      "extension": "jpg",
      "id": "10",
      "fileType": "IMG",
      "name": "vertical-1_XXX"
    },
    {
      "extension": "mp4",
      "id": "15",
      "fileType": "VID",
      "name": "aaa"
    },
    {
      "extension": "pdf",
      "id": "19",
      "fileType": "DOC",
      "name": "ccc"
    }
  ]
};

const filterFile = (type) => {
  let result = [];
  data.assets.map((o) => {
    if (o.fileType === type) {
      result = [...result, o];
    }
  });
  return result;
}
console.log(filterFile('IMG'));

1 Comment

i don't think that performs any better than filter. Plus there's an unnecessary mutation due to it.

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.