Oam Psy
Oam Psy

Reputation: 8663

Filtering object keys and values

I have a object which looks like this:

var myObj = {
    "account id": "123TRANS",
    "account name": "Optimus Prime",
    "creator id": "Prime",
    "contact tel": 08915243209,
    "house no": 209,
    "is registered": true,
    "price": 398.83,
    "first name": "paul",
    "last name": "jones",
    "dob": "27-Jun-1978 00:00:00",
    "previous orders": true,
    "average price": 123.78,
    "lace colour": "blue",
    "first line": "Jupiter Close",
    "city": "New York",
    "alias": "Jupiter",
    "role": "Accountant"
}

On my HTML page I have an <input> field where users can type in text (minimum of 3 characters required). I was to be able to filter the entire object above on matches for the entered string.

For example, if string = "Prime" response is:

{
    "account name": "Optimus Prime",
    "creator id": "Prime",
}

For example, string = "acc" response is:

{
    "account id": "123TRANS",
    "account name": "Optimus Prime",
    "role": "Accountant"
}

For example, string = "209" response is:

{
    "contact tel": 08915243209,
    "house no": 209,
}

I have tried using .reduce, however this gives mixed results:

var myObject = {from above};
var filter = 'prime';

myObject = Object.keys(myObject).reduce((r, e) => {
    // Avoid int/float/bool and check key + value
    if(e !== 'contact tel' &&
       e !== 'house no' &&
       e !== 'previous orders') {

       // Check Key or Value for filter
       if(myObject[e].toLowerCase().includes(filter.toLowerCase) || e.toLowerCase().includes(filter.toLowerCase())) {
           r[e] = myObject[e]
       }
    }

    // Check int/float/bool and check key
    if(e === 'contact tel' ||
       e === 'house no' ||
       e === 'previous orders') {

       // Check Key or Value for filter
       if(e.toLowerCase().includes(filter.toLowerCase())) {
           r[e] = myObject[e]
       }
    }

    return r;  

}, {})

I have access to lodash too however could not find anything that checks keys AND values, only key OR value.

Upvotes: 2

Views: 386

Answers (3)

Ori Drori
Ori Drori

Reputation: 192477

You can use _.pickBy() to get a sub object based on key or value containing a text:

const findInObj = (obj, str) => {
  const pattern = new RegExp(str, 'i')
  return _.pickBy(obj, (v, k) => pattern.test(v) || pattern.test(k))
}

const myObj = {"account id":"123TRANS","account name":"Optimus Prime","creator id":"Prime","contact tel":8915243209,"house no":209,"is registered":true,"price":398.83,"first name":"paul","last name":"jones","dob":"27-Jun-1978 00:00:00","previous orders":true,"average price":123.78,"lace colour":"blue","first line":"Jupiter Close","city":"New York","alias":"Jupiter","role":"Accountant"}

const result = findInObj(myObj, 'acc')

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Upvotes: 1

AB Udhay
AB Udhay

Reputation: 753

`

var myObj = {
    "account id": "123TRANS",
    "account name": "Optimus Prime",
    "creator id": "Prime",
    "contact tel": 08915243209,
    "house no": 209,
    "is registered": true,
    "price": 398.83,
    "first name": "paul",
    "last name": "jones",
    "dob": "27-Jun-1978 00:00:00",
    "previous orders": true,
    "average price": 123.78,
    "lace colour": "blue",
    "first line": "Jupiter Close",
    "city": "New York",
    "alias": "Jupiter",
    "role": "Accountant"
},
filterString = 'prime';
Object.entries(myObj)
  .filter((elem) => {
    return (elem.join('').toLowerCase().indexOf(filterString) > -1);
  })
  .reduce((accumulator, [key, value]) => {
    accumulator[key] = value;
    return accumulator;
  }, {});`

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386736

You could get the entries, filter the keys and values and build a new object as result.

function find(object, search) {
    search = search.toLowerCase();
    return Object.assign(...Object
        .entries(object)
        .filter(a => a.some(s => s.toString().toLowerCase().includes(search)))
        .map(([k, v]) => ({ [k]: v }))
    );
}

var myObj = { "account id": "123TRANS", "account name": "Optimus Prime", "creator id": "Prime", "contact tel": "08915243209", "house no": 209, "is registered": true, price: 398.83, "first name": "paul", "last name": "jones", dob: "27-Jun-1978 00:00:00", "previous orders": true, "average price": 123.78, "lace colour": "blue", "first line": "Jupiter Close", city: "New York", alias: "Jupiter", role: "Accountant" };

console.log(find(myObj, 'ACC'));

Upvotes: 1

Related Questions