Is there a way to sort an elastic query using JavaScript?

I'm trying to modify my elastic query by adding and removing items from an array. In the array below, I want to remove the element that contains 'item2'. How can I achieve this by checking if the key 'item2' exists and then deleting that specific item?

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

Answer №1

Is this what you are looking for?

let data = [{
        details: {
            info1: [
                '1111111111111',
                '2222222222222',
                '3333333333333',
                '4444444444444'
            ]
        }
    },
    {
        details: {
            info2: [
                'aaaaaaaaaaaaaaaaaaaaaaa',
                'bbbbbbbbbbbbbbbbbbbbbbb',
                'ccccccccccccccccccccccc'
            ]
        }
    },
    {
        limits: {
            info3: {
                start: 0,
                end: 99999
            }
        }
    }
];

const result = data.filter((element) => !element[Object.keys(element)[0]].hasOwnProperty('info2'))

console.log(JSON.stringify(result))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to identify images within a message sent by users to the server and provide a response accordingly

Apologies for my not-so-great English I am currently learning JavaScript and I am trying to detect an image in a message sent by users from the server, and reply with that image embedded in a bot message. However, message.content is not working for this p ...

storage location for data in JSON format

When using content type : "application/x-www-form-urlencoded", the result is stored in Request.Form in Asp MVC. However, for "application/json", the storage location cannot be found. Below is the code that is being used: AJAX part // reading form da ...

`Why is it important to debug javascript code?`

I have some outdated JavaScript code that surprisingly still works in Internet Explorer from 2000-2002, but refuses to function properly in browsers like Firefox, Chrome, and Opera. I've come across various browser quirks where different browsers inte ...

Tips on redirecting the treeview menu using Material-UI 4

Currently, I am utilizing Material UI 4's Treeview component. However, I am struggling to figure out how to include a parameter that allows me to redirect to other pages when the user clicks on an item. In the past, I relied on a different method. Ch ...

Showing a div with 2 unique data attributes - such as displaying Currency and Quantity

My E-Commerce website is created using HTML, JavaScript, and PHP. On the product details page, users can add products to their cart, so I show the total cart value. I need the total amount displayed in a decimal number format (10,2). Currently, when a u ...

Ways to transmit the appropriate model

Using AJAX, I am sending a model to the server via a POST request. I have defined a model and a controller method for processing it. public class TestDto: BaseDto { [Required] public ProductGalleryDto GalleryProduct { get; set; } public int ...

Creating a Matrix-style display of 2D arrays using Python

If I possess a matrix within a numpy array in Python In [3]: my_matrix Out[3]: array([[ 2., 2., 2., 2., 2., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 2., 2., 2., 2., 0., 0., ...

What is the best way to access the final iteration of a foreach loop when the overall count is not known

I am trying to display a variable during the last iteration of a foreach loop, only when a specific condition is met within the loop. While I could easily achieve this by echoing $var outside the foreach statement, my goal is to accomplish it within the l ...

Leveraging keyboard input for authentication in Angular

Would it be possible to modify a button so that instead of just clicking on it, users could also enter a secret passphrase on the keyboard to navigate to the next page in Angular? For example, typing "nextpage" would take them to the next page. If you&apo ...

Leverage Jasmine for testing a jQuery plugin

I'm currently utilizing the angular-minicolors library () within an angular controller: angular.element("myelement").minicolors({ position: 'top left', change: function() { //custom code to run upon color change } }) Wh ...

"Utilizing JSON data to implement custom time formatting on the y-axis with AmCharts

Looking to convert minutes to hh:mm:ss format in my JavaScript code var allDataTime = [{ date: new Date(2012, 0, 1), "col": "LONG CALL WAITING", "duration1": '720', "duration2": '57', "duration3": ...

How do I incorporate global typings when adding type definitions to an npm module?

Suppose I create a node module called m. Later on, I decide to enhance it with Typescript typings. Luckily, the module only exports a single function, so the m.d.ts file is as follows: /// <reference path="./typings/globals/node/index.d.ts" /> decl ...

Sort objects based on a specific property that they possess

In my current setup, I have an array of objects structured as shown below: $scope.people = [{name: {last:"Doe", first:"John"}}, {name: {last:"Smith", first:"Joe"}}]; My goal is to implement a live filt ...

Executing an event in Javascript or triggering with Jquery- the process of initiating an event once a value is sent to an input box by Javascript

How do you trigger an event when a JavaScript-passed value is entered into an input box? <!DOCTYPE html> <html> <body> <p>Type something in the text field to activate a function.</p> <input type="text" id="myInput" oninp ...

What steps can be taken to enable users to draw a path on a Google Map?

I'm working on a new project for a Facebook app that will allow users to map out their marathon route using Google Maps. I plan to utilize mySQL database records to store fixed points along the path (such as specific locations based on latitude and lo ...

How can you efficiently manage Access & Refresh tokens from various Providers?

Imagine I am allowing my users to connect to various social media platforms like Facebook, Instagram, Pinterest, and Twitter in order to use their APIs. As a result, I obtain access tokens for each of these providers. Based on my research, it seems advisa ...

Change the price directly without having to click on the text field

Our Magento marketplace site is currently utilizing the code below for updating prices. However, we are looking to make the price field editable without requiring a click on the textfield. This means that users should be able to edit the price directly wi ...

What is causing the data added to an array to vanish after the forEach loop within the useEffect hooks?

Below is the code snippet: const Tabs = ({data, scrollX}) => { const [measures, setMeasures] = useState([]); const containerRef = useRef({}); let measureMentArray = []; useEffect(() => { data && data.forEach(item => { ...

Solutions for concealing the current div when clicking on a new div that reveals a fresh one

Is there a way to hide the current div once you click on a new div that reveals another one? The code below toggles the display of the div, but what I am attempting to achieve is that when you click on a new item after clicking on the first one, the first ...

Using Reactjs to Send Emails

Trying to incorporate the SmptJs API for email sending using JavaScript has been quite successful in a simple HTML setup. However, I am facing challenges when attempting to integrate it into a ReactJs component! The API documentation and link can be found ...