-1

Let's say there is an array of objects like,

[
    {
        "fname": "Anne",
        "lname": "Walker",
        "email": "[email protected]",
    },
    {
        "fname": "Peterson",
        "lname": "Dalton",
        "email": "[email protected]",
    },
    {
        "fname": "Velazquez",
        "lname": "Calderon",
        "email": "[email protected]",
    },
    {
        "fname": "Norman",
        "lname": "Reed",
        "email": "[email protected]",
    }
]

I want to create a search filter which searches by the only fname and lname. There was a question like this before asked but it search through all the fields

how to create React search filter for search multiple object key values

Can anyone help me on this ?

1
  • good job for looking for existing solution. That said, in the link you provided, the filtering occurs on a given field provided by user (see this.setState). Either fname or lname or email. Is this fine for you or you want to apply several filter at the same time? Commented Nov 14, 2019 at 7:34

3 Answers 3

4

Is this ok?

const json = [
    {
        "fname": "Anne",
        "lname": "Walker",
        "email": "[email protected]",
    },
    {
        "fname": "Peterson",
        "lname": "Dalton",
        "email": "[email protected]",
    },
    {
        "fname": "Velazquez",
        "lname": "Calderon",
        "email": "[email protected]",
    },
    {
        "fname": "Norman",
        "lname": "Reed",
        "email": "[email protected]",
    }
];
function search(str) {
    const keyword = str.toLowerCase();
    return json.filter(x => x.fname.toLowerCase().includes(keyword) || x.lname.toLowerCase().includes(keyword));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. that was really helpful
You're welcome. BTW, If you want to search for a keyword that matches either the lname or fname, use the code above. But if you need to write a function that searches the objects based on both fname AND lname, you must use @corrado4eyes 's answer. Also notice that I use .toLowerCase() to search case-insensitive. If you need your search to be Case-Sensitive remove the usage of .toLowerCase() Happy coding.
1

You should propose an example you tried and say "Hey guys I tried this on these data, but it did not work.". However is an array of object, so what you could do is:

const magicFunction = (name, lastName, array) => {
    return array.filter((el) => el.fname === name && el.lname === lastName);
}

Comments

1

You can use lodash library. Use find or filter functions. Find - for single result and Filter for multiple results Example-

let _ = require('lodash');
let data = [
    {
        "fname": "Anne",
        "lname": "Walker",
        "email": "[email protected]",
    },
    {
        "fname": "Peterson",
        "lname": "Dalton",
        "email": "[email protected]",
    },
    {
        "fname": "Velazquez",
        "lname": "Calderon",
        "email": "[email protected]",
    },
    {
        "fname": "Norman",
        "lname": "Reed",
        "email": "[email protected]",
    }
]
let result = _.find(data, { fname: 'Norman', lname: 'Reed'})

Another alternative without library

let res = data.find(o => o.fname == "Norman" && o.lname == "Reed")
console.log(res)

2 Comments

No need to include a library for a basic requirement. Can be achieved using Array.prototype.find
Thanks people, For your time

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.