Search code examples
arraysstringjavascript-objectslodash

Find string in array inside array of objects


I have a complex declarative config array of objects and i need to get some data from it. this is my array:

units = [{
    'hpid': '787',
    'hpr': 'Hotel Apartamento Solverde',
    'pt': {
      'CONVERSION': [],
      'HOME_PAGE': [],
      'PRODUCT': [],
      'SEARCH': [],
      'SHOPPING_CART': [],
      'TRACKING': []
    }
  }, {
    'hpid': '130',
    'hpr': 'Hotel Algarve Casino',
    'pt': {
      'CONVERSION': [],
      'HOME_PAGE': ['/hotel-algarve-casino/'],
      'PRODUCT': [
        '/hotel-algarve-casino/quartos/quarto-standard-vista-avenida/',
        '/hotel-algarve-casino/quartos/quarto-superior-vista-mar/',
        '/hotel-algarve-casino/quartos/quarto-familiar-vista-mar/',
        '/hotel-algarve-casino/quartos/suite-vista-avenida/',
        '/hotel-algarve-casino/quartos/suite-vista-mar/',
      ],
      'SEARCH': [
        '/hotel-algarve-casino/quartos/',
        '/en/hotel-algarve-casino/rooms/'
      ],
      'SHOPPING_CART': [],
      'TRACKING': []
    }
  }, {
    'hpid': '300',
    'hpr': 'Hotel Casino Chaves',
    'pt': {
      'CONVERSION': [],
      'HOME_PAGE': ['/hotel-casino-chaves/'],
      'PRODUCT': [
        '/hotel-casino-chaves/quartos/quarto-standard-vista-avenida/',
        '/hotel-casino-chaves/quartos/quarto-superior-vista-mar/',
        '/hotel-casino-chaves/quartos/quarto-familiar-vista-mar/',
        '/hotel-casino-chaves/quartos/suite-vista-avenida/',
        '/hotel-casino-chaves/quartos/suite-vista-mar/',
      ],
      'SEARCH': [
        '/hotel-casino-chaves/quartos/',
        '/en/hotel-casino-chaves/rooms/'
      ],
      'SHOPPING_CART': [],
      'TRACKING': []
    }
  }, {
    'hpid': '759',
    'hpr': 'Hotel Solverde Spa and Wellness Center',
    'pt': {
      'CONVERSION': [],
      'HOME_PAGE': [],
      'PRODUCT': [],
      'SEARCH': [],
      'SHOPPING_CART': [],
      'TRACKING': []
    }
  }
];

and need to find the atribute name that contains the string ex:/en/hotel-casino-chaves/rooms/

Also i need the value of 'hpid' attribute of the previous search.

I apreciate your help. Thanks.

then i need to find


Solution

  • This should do the trick:

    In the callback of Array.reduce check if for the current element c one or more of the arrays in c.pt contain the search string.

    If yes, add the c.hpid and the name of the properties containing the search string to the result.

    units = [{
        'hpid': '787',
        'hpr': 'Hotel Apartamento Solverde',
        'pt': {
          'CONVERSION': [],
          'HOME_PAGE': [],
          'PRODUCT': [],
          'SEARCH': [],
          'SHOPPING_CART': [],
          'TRACKING': []
        }
      }, {
        'hpid': '130',
        'hpr': 'Hotel Algarve Casino',
        'pt': {
          'CONVERSION': [],
          'HOME_PAGE': ['/hotel-algarve-casino/'],
          'PRODUCT': [
            '/hotel-algarve-casino/quartos/quarto-standard-vista-avenida/',
            '/hotel-algarve-casino/quartos/quarto-superior-vista-mar/',
            '/hotel-algarve-casino/quartos/quarto-familiar-vista-mar/',
            '/hotel-algarve-casino/quartos/suite-vista-avenida/',
            '/hotel-algarve-casino/quartos/suite-vista-mar/',
          ],
          'SEARCH': [
            '/hotel-algarve-casino/quartos/',
            '/en/hotel-algarve-casino/rooms/'
          ],
          'SHOPPING_CART': [],
          'TRACKING': []
        }
      }, {
        'hpid': '300',
        'hpr': 'Hotel Casino Chaves',
        'pt': {
          'CONVERSION': [],
          'HOME_PAGE': ['/hotel-casino-chaves/'],
          'PRODUCT': [
            '/hotel-casino-chaves/quartos/quarto-standard-vista-avenida/',
            '/hotel-casino-chaves/quartos/quarto-superior-vista-mar/',
            '/hotel-casino-chaves/quartos/quarto-familiar-vista-mar/',
            '/hotel-casino-chaves/quartos/suite-vista-avenida/',
            '/hotel-casino-chaves/quartos/suite-vista-mar/',
          ],
          'SEARCH': [
            '/hotel-casino-chaves/quartos/',
            '/en/hotel-casino-chaves/rooms/'
          ],
          'SHOPPING_CART': [],
          'TRACKING': []
        }
      }, {
        'hpid': '759',
        'hpr': 'Hotel Solverde Spa and Wellness Center',
        'pt': {
          'CONVERSION': [],
          'HOME_PAGE': [],
          'PRODUCT': [],
          'SEARCH': [],
          'SHOPPING_CART': [],
          'TRACKING': []
        }
      }
    ];
    
    
    let result = units.reduce((a,c) => {
      let entries = Object.entries(c.pt)
        .filter(y => y[1].includes('/en/hotel-casino-chaves/rooms/'));
        
      for (let e of entries) {
        a.push({ hpid: c.hpid, prop: e[0]});
      }
      return a;
    }, []);
    
    console.log(result);