Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I filter elastic query in javascript?

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
        }
      }
    }
  ];

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading