Home > OS >  How can I filter elastic query in javascript?
How can I filter elastic query in javascript?

Time:11-26

I want to filter elastic query. I added item but i can't remove item. My array is below. For example, I want to delete the element containing item2 in my array. I want to check if the item2 key exists and delete that item if it is. How can I do?

let array = [
    {
      terms: {
        item1: [
          '1111111111111',
          '2222222222222',
          '3333333333333',
          '4444444444444'
        ]
      }
    },
    {
      terms: {
        item2: [
          'aaaaaaaaaaaaaaaaaaaaaaa',
          'bbbbbbbbbbbbbbbbbbbbbbb',
          'ccccccccccccccccccccccc'
        ]
      }
    },
    {
      range: {
        item3: {
          from: 0,
          to: 99999
        }
      }
    }
  ];

CodePudding user response:

Something like this?

let array = [{
        terms: {
            item1: [
                '1111111111111',
                '2222222222222',
                '3333333333333',
                '4444444444444'
            ]
        }
    },
    {
        terms: {
            item2: [
                'aaaaaaaaaaaaaaaaaaaaaaa',
                'bbbbbbbbbbbbbbbbbbbbbbb',
                'ccccccccccccccccccccccc'
            ]
        }
    },
    {
        range: {
            item3: {
                from: 0,
                to: 99999
            }
        }
    }
];

const filtered = array.filter((el) => !el[Object.keys(el)[0]].hasOwnProperty('item2'))

console.log(JSON.stringify(filtered))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related