代码之家  ›  专栏  ›  技术社区  ›  Leonel Matias Domingos

在对象数组内的数组中查找字符串

  •  0
  • Leonel Matias Domingos  · 技术社区  · 3 年前

    我有一个复杂的对象声明性配置数组,我需要从中获取一些数据。 这是我的数组:

    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': []
        }
      }
    ];
    
    

    并且需要找到包含字符串ex: /en/hotel-casino-chaves/rooms/

    此外,我还需要上一次搜索的“hpid”属性的值。

    我感谢你的帮助。 谢谢

    那么我需要找到

    1 回复  |  直到 3 年前
        1
  •  1
  •   derpirscher    3 年前

    这应该会奏效:

    在的回调中 Array.reduce 检查当前元素是否 c 中的一个或多个阵列 c.pt 包含搜索字符串。

    如果是,请添加 c.hpid 以及将包含搜索字符串的属性的名称添加到结果中。

    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);