Home > OS >  How to filter JSON in JS react
How to filter JSON in JS react

Time:08-17

Need to filter this JSON file by "subject":

  "mailbox":[
                     {
                        "mbname":"[email protected]",
                        "email_list":[
                            {
                                "recipient":    "[email protected]",
                                "subject":      "This is a test mail",
                                "from":         "[email protected]",
                                "received":     "17.08.2022",
                                "file_name": "testfile.txt"
                            } ,
                            {
                                "recipient":    "[email protected]",
                                "subject":      "This is a test mail",
                                "from":         "[email protected]",
                                "received":     "17.08.2022",
                                "file_name": ".testfile.txt"
                            }
                        ]
                        
                        } ]
                    

I did it for mbname it make senese how to do it, but i case of email_list.subject.includes(key) it is doesnt work

 const filterData = (key, data) => {
  const userOnly = data.filter(({mbname}) => mbname.includes(key));
 
  return userOnly[0];
}

CodePudding user response:

Didn't really understood what you mean, but if you want to filter the emails of the email_list that contains a certain substring, you may take a shot like this:

const substr = 'hi!';

const email_list = [{
        "recipient":    "[email protected]",
        "subject":      "This is a test mail",
        "from":         "[email protected]",
        "received":     "17.08.2022",
        "file_name": "testfile.txt"
    }, {
        "recipient":   "[email protected]",
        "subject":      "This is a test mail, hi!",
        "from":         "[email protected]",
        "received":     "17.08.2022",
        "file_name": ".testfile.txt"
    }
];

const filteredList = email_list.filter(({ subject }) => subject?.includes(substr));

// :)
console.log(filteredList);

Also, if you just need the first data found that includes the specified filter, you may like to use .find() instead of .filter(). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

CodePudding user response:

Create a function that accepts an array, a key, and a value, and then filter the array returning each object where the key/value match.

const json = '{"mailbox":[{"mbname":"[email protected]","email_list":[{"recipient":"[email protected]","subject":"Subject1","from":"[email protected]","received":"17.08.2022","file_name":"testfile.txt"},{"recipient":"[email protected]","subject":"Subject2","from":"[email protected]","received":"17.08.2022","file_name":".testfile.txt"}]}]}';

const data = JSON.parse(json);

function filter(arr, key, value) {
  return arr.filter(obj => obj[key] === value);
}

const [{ email_list }] = data.mailbox;

console.log(filter(email_list, 'subject', 'Subject1'));
console.log(filter(email_list, 'subject', 'Subject2'));
console.log(filter(email_list, 'recipient', '[email protected]'));
console.log(filter(email_list, 'recipient', '[email protected]'));
console.log(filter(email_list, 'from', '[email protected]'));

CodePudding user response:

I didn't understand your objective very well, but with the solution below you will receive the mail whose subject corresponds to the intended one, and the mbname information will be kept.

const list = [
                     {
                        "mbname":"[email protected]",
                        "email_list":[
                            {
                                "recipient":    "[email protected]",
                                "subject":      "This is a test mail",
                                "from":         "[email protected]",
                                "received":     "17.08.2022",
                                "file_name": "testfile.txt"
                            } ,
                            {
                                "recipient":    "[email protected]",
                                "subject":      "This is a test mail",
                                "from":         "[email protected]",
                                "received":     "17.08.2022",
                                "file_name": ".testfile.txt"
                            }
                        ]
                        
                        } ];
const filterToSubject = (data, infoToFilter) =>
  data
    .reduce((acc, curr) => {
      const { email_list = [], ...rest } = curr;
      return [...acc, ...email_list.map((el) => ({ ...rest, ...el }))];
    }, [])
    .filter(({ subject }) => subject.includes(infoToFilter));
filterToSubject(list, 'This is')
  • Related