1

What i am trying to do is filter one object using another. I have it working for the keys such as name or email but the problem is for the address field where it needs to check the value recursively.

I have the following object:

  {
    "id" : 1,
    "name": "Michael",
    "surname": "Smith",
    "emailAddress": "m.smith@gmail.com",
  },
  {
    "id": "2",
    "name": "Sam",
    "surname": "Smith",
    "emailAddress": "s.smith@gmail.com",
    "address": {
      "text": "123 Fake Street, Some Place, United kingdom",
      "street_number": "123",
      "route": "Fake Street",
      "locality": "Some Place",
      "country": "UK",
      "postcode": "HP18",
      "lat": 51.648858999292999,
      "lng": 4.08207329992999786,

    }
  },
  {
    "id": 3,
    "name": "Josh",
    "surname": "Fradley",
    "emailAddress": "myemai@gmail.com",
    "address": {
      "text": "1 Apple Park Way, Cupertino, CA, USA",
      "street_number": "1",
      "route": "Apple Park Way",
      "locality": "Cupertino",
      "administrative_area_level_1": "California",
      "country": "US",
      "postcode": "95014",
      "lat": 37.3346326,
      "lng": -122.01268240000002,
    }
  }
]

Example search object

{
  "name": "",
  "phone": "",
  "address": "",
  "emailAddress": "myemai@gmail.com"
}

The code i use for the email and name fields

  let res =  array.filter(o => 
    Object.keys(searches).every(k => 
      o[k] && o[k].length ? removeDiacritics(o[k].toLowerCase()).indexOf(removeDiacritics(searches[k].toLowerCase())) >= 0 : true
   ));

What i am struggling with is how to search the address key which is an object, for example if i search "address": "Fake Street" it should return id 2

Any suggestions?

EDIT: I forgot to mention that the filters array is dynamic so i cant hard code in "address", i used here as an example of an object

Josh Fradley
  • 545
  • 1
  • 10
  • 23

1 Answers1

0

You could take a local function and check the value of the object for nested objects.

var data = [{ id: 1, name: "Michael", surname: "Smith", emailAddress: "m.smith@gmail.com" }, { id: "2", name: "Sam", surname: "Smith", emailAddress: "s.smith@gmail.com", address: { text: "123 Fake Street, Some Place, United kingdom", street_number: "123", route: "Fake Street", locality: "Some Place", country: "UK", postcode: "HP18", lat: 51.648858999293, lng: 4.082073299929998 } }, { id: 3, name: "Josh", surname: "Fradley", emailAddress: "myemai@gmail.com", address: { text: "1 Apple Park Way, Cupertino, CA, USA", street_number: "1", route: "Apple Park Way", locality: "Cupertino", administrative_area_level_1: "California", country: "US", postcode: "95014", lat: 37.3346326, lng: -122.01268240000001 } }],
    search = { name: "", phone: "", address: 123, emailAddress: "" },
    result = data.filter(o => Object.entries(search).every(([k, v]) => {
        function check(value) {
            return value && typeof value === 'object'
                ? Object.values(value).some(check)
                : value.toString().includes(v);
        }

        if (v === '') return true;
        if (k in o) return check(o[k]);
    }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • this is so close but i want address to search through all the keys in address, not having to specific street_number – Josh Fradley Dec 13 '19 at 17:28
  • please add a search object and the result. – Nina Scholz Dec 13 '19 at 17:54
  • {address : 123 } Would return id 2, for example as 123 is in text and street number { id: "2", name: "Sam", surname: "Smith", emailAddress: "s.smith@gmail.com", address: { text: "123 Fake Street, Some Place, United kingdom", street_number: "123", route: "Fake Street", locality: "Some Place", country: "UK", postcode: "HP18", lat: 51.648858999293, lng: 4.082073299929998 } – Josh Fradley Dec 13 '19 at 19:36
  • you want to search the address in obj1 key with all the keys in address object obj1 and you don't want to hardcode the key address... my question is always address may become object or other keys may be an object too – Amir Rezvani Dec 13 '19 at 19:51
  • No addres is only example could be products or any over object – Josh Fradley Dec 13 '19 at 19:55
  • TypeError: value is undefined when searching for {address : 123 } – Josh Fradley Dec 14 '19 at 07:43
  • you could treat all values as strings. please see edit. – Nina Scholz Dec 14 '19 at 09:44
  • that works perfectly i added: if (typeof value === 'undefined') { return false; } – Josh Fradley Dec 14 '19 at 13:13