sophisticated method for sorting through data within an array of arrays

How can data be filtered from an array of arrays?

Below is an example to help explain the process.

To filter the data, use startnumber and endnumber in the query.

const data =  [
{
  "name": "x",
  "points": [
    [100, 50, 1],        //[number, value, bit]       
    [150, 51, 0],
    [170, 52, 1],
    [200, 53, 0]
  ]
},
{
  "name": "y",
  "points": [
    [60, 50, 1],
    [100, 5, 1],
    [150, 6, 0],
    [170, 7, 1],
    [200, 53, 1]
  ]
},
{
  "name": "z",
  "points": [
    [300, 50, 1],
    [350, 51, 0],
    [370, 52, 1],
    [400, 53, 1]
  ]
}
]

// To find records with names x & y and numbers between 100 to 170

const names = ["x", "y"];
const startnumber = 100;
const endnumber = 170;

const finalResult= [];
for(const n of names){
  console.log('name', n);
  console.log('startnumber', startnumber)
  console.log('endnuumber', endnumber)
  const result = data.find(x=>x.name === n)
  
  // How can startnumber and endnumber be applied in the query above? Or is there another elegant solution?

  if(result){
    finalResult.push('result', result);
  }
}

if(finalResult.length){
  console.log(finalResult);
}

The expected result should be

[
  {
    "name": "x",
    "points": [
      [100, 50, 1],
      [150, 51, 0],
      [170, 52, 1],
    ]
  },
  {
    "name": "y",
    "points": [
      [100, 5, 1],
      [150, 6, 0],
      [170, 7, 1],
    ]
  }
]

Answer №1

const filteredResults = data.filter(elem => !!names.find(name => name == elem.name))

Explanation:

  1. data.filter is used to iterate through each element in the array data.
  2. names.find is applied to each element in the array names to find a matching value.
  3. The double negation (!!) is utilized since find returns an element, converting it into a boolean.
  4. The resulting array contains elements from data with names that match the values in names.

const data = [
{
  "name": "x",
  "points": [
    [100, 50, 1],
    [150, 51, 0],
    [170, 52, 1],
    [200, 53, 0]
  ]
},
{
  "name": "y",
  "points": [
    [60, 50, 1],
    [100, 5, 1],
    [150, 6, 0],
    [170, 7, 1],
    [200, 53, 1]
  ]
},
{
  "name": "y",
  "points": [
    [60, 50, 1],
    [200, 53, 1]
  ]
},
{
  "name": "z",
  "points": [
    [300, 50, 1],
    [350, 51, 0],
    [370, 52, 1],
    [400, 53, 1]
  ]
}
]

// Want to filter records with names 'x' and 'y', where number is between 100 and 170.

const names = ["x", "y"];
const startNumber = 100;
const endNumber = 170;

const filteredResults = data
  .filter(elem => !!names.find(name => name == elem.name))
  .map(elem => {
    return {
      name: elem.name,
      points: elem.points.filter(point => point[0] >= startNumber && point[0] <= endNumber)
    }
  })
  .filter(elem => elem.points.length > 0)

console.log(filteredResults)

Answer №2

This method offers a sophisticated approach:

const result = data.reduce((accumulator, current) => {
  const { title, values } = current
  const filteredValues = points.filter(value => value[0] >= startValue && value[0] <= endValue)
  return titles.includes(title) && filteredValues.length > 0 ? accumulator.concat({ title, values: filteredValues }) : accumulator
}, [])

Answer №3

When analyzing the task and the two provided data structures, it becomes apparent that a dual-layered filtering process is required for each data element.

  1. The first step involves identifying the data item based on its name (obtained from the additional names array).
  2. The second step entails filtering the points array of each data item to determine if the first element in the points array falls within a specified number range (defined by the supplementary startnumber and endnumber values).

Consequently, an effective strategy was to utilize the reduce function on the given data array with a reducer function that handles these two tasks for each data item.

The initial value for the reducer function serves as a configuration and collection object that includes...

  • A name lookup mechanism (implemented as a Map instance) for the specific data items to be processed.
  • The startnumber and endnumber range values designated as lowerNumber and upperNumber, respectively.
  • An array named result where only those data items meeting all criteria are stored.

function collectItemsByNameWhichHavePointsInNumberRange(collector, item) {
  const { nameLookup, lowerNumber, upperNumber, result } = collector;
  let { name, points } = item;

  // ... code snippet to gather items by name ...
  if (nameLookup.has(name)) {
    points = points
      .filter(([number]) =>
        (number >= lowerNumber) && (number <= upperNumber)
      );

    // ... filter out items with points falling within the defined number range ...

    if (points.length >= 1) {
      result
        .push({
          ...item,
          points,
        });
    }
  }
  return collector;
}

const data = [{
  name: "x",
  points: [
    [100, 50, 1],
    [150, 51, 0],
    [170, 52, 1],
    [200, 53, 0],
  ],
}, {
  name: "y",
  points: [
    [60, 50, 1],
    [100, 5, 1],
    [150, 6, 0],
    [170, 7, 1],
    [200, 53, 1],
  ],
}, {
  name: "z",
  points: [
    [300, 50, 1],
    [350, 51, 0],
    [370, 52, 1],
    [400, 53, 1],
  ],
}];

const names = ["x", "y"];
const startnumber = 100;
const endnumber = 170;

const { result } = data
  .reduce(collectItemsByNameWhichHavePointsInNumberRange, {
    nameLookup: new Map(names.map(value => [value, value])),
    lowerNumber: startnumber,
    upperNumber: endnumber,
    result: [],
  });

console.log({ result });
.as-console-wrapper { min-height: 100%!important; top: 0; }

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

Retrieve all entries and merge a field with aggregated information in Mongoose (MongoDB)

I am faced with the challenge of working with two Mongo collections, Users and Activities. The Activities collection consists of fields such as createdAt (type Date), hoursWorked (type Number), and a reference to the user through the user field. On the oth ...

Enhance the performance of node.js when processing data from a massive file

My dilemma lies in the challenge of reading and processing a large file with numerous rows. When dealing with small files under 50kb, everything runs smoothly. However, I am faced with a 15MB file for a programming competition, which serves as a hard input ...

Instead of using a v-if condition, add a condition directly in the Vue attribute

Apologies for the unclear title, as I am unsure of what to name it with regards to my current issue, I am attempting to create a component layout using vuetify grid. I have a clear idea of how to do this conventionally, like so: <template> <v-fl ...

How to trigger a force reload on a VueJS route with a different query parameter?

Is there a method to refresh the page component when two pages utilize the same Component? I have encountered an issue where the router does not reload and the previous edit values persist. { path: "/products/new", component: ProductPage, meta: { ...

What is the best way to connect click events with ExtJS template elements?

Is there a way to assign a click event to each link tag without directly embedding onclick=.... in the XTemplate code? new Ext.XTemplate( '<ul>', '<tpl for="."><li><a href="#{anchor}">{text}</a></li& ...

Choose or deselect images from a selection

I am currently working on a feature for an album creation tool where users can select photos from a pool of images and assign them to a specific folder. However, I'm facing difficulty in selecting individual photos and applying customized attributes t ...

Differences seen in display when using Internet Explorer's prependTo feature

Here is some code that I am working with:- <ul id="list1"> <li class="a">item 1</li> <li class="a">item 2</li> <li class="b">item 3</li> <li class="b">item 4</li> </ul> Additiona ...

Tips for preserving component state during page rerenders or changes

I created a counter with context API in React, and I'm looking for a way to persist the state even when the page is changed. Is there a method to store the Counter value during page transitions, similar to session storage? My app utilizes React Router ...

Utilize Mapbox-GL.JS to animate several points along designated routes

I'm encountering issues with the following example: Animate a point along a route My goal is to add another point and routes in the same map container. Here's what I've tried so far: mapboxgl.accessToken = 'pk.eyJ1IjoicGFwYWJ1Y2t ...

Implementing a function or template from one component into another within a Vue.js application, despite lacking a direct connection between the two components

Working on my Vue.js app, I encountered an interesting challenge: The layout of the app is simple: it consists of a header, a main view, and a navigation section at the bottom. Depending on the current page the user is on, I want to display a main action ...

Passing data from a child component to a parent component in Vue 3: How

Struggling with Vue 3 app authentication through event-emission from child to parent. Below is a snippet of the code: Child <template> <form class="msform"> <input @click="goToLogin" type="button" name=&q ...

The checkbox will be automatically checked whenever there is any modification in the textbox

I'm currently working on a Grid view with a checkbox and two textboxes. What I want is for the checkbox to be automatically checked whenever there is a change in one of the textbox values, for example switching from 0 to 1. This project is being devel ...

Encountering an illegal invocation error in jQuery

Recently delving into the world of jQuery, I am attempting to call a C# function from JavaScript using AJAX and jQuery. Additionally, I need to pass some parameters while making the call to the C# function. Here is how I am attempting to achieve this: var ...

I am interested in extracting information from a public Facebook post

Is it possible to scrape or utilize the FB API to retrieve data from a public profile's wall post? By inspecting the element on the URL, you can see most of the data as well as the ajax calls for infinite scrolling on the wall. How could one go about ...

Javascript: regular expression to validate alphanumeric and special characters

Looking to create a regular expression for a string (company/organization name) with the following conditions: No leading or trailing spaces No double spaces in between Shouldn't allow only a single character (alphanumeric or whitelisted) Can start ...

How can I attach an existing event to a dynamically loaded element using AJAX?

In the main page of my website, there is a button: <button class="test">test</button> Additionally, I have included the following script in my code: $('.test').on('click',function(){ alert("YOU CLICKED ME"); } ...

Developing Modules in NodeJS using Constructors or Object Literals

I am currently developing a nodejs application that needs to communicate with various network resources, such as cache services and databases. To achieve this functionality, I have created a module imported through the require statement, which allows the a ...

Is there a way to live filter results using Vue?

Looking to apply a filter on my input v-model to search through a list of products. The current filter only shows items with an exact match to the input. How can I adjust it to display partial matches as the user types? Vue.filter('byName', fun ...

Encountering Err_Connection_Refused while working with MVC WebAPI 2 and AngularJS

Seeking guidance on WebAPI, AngularJS, and .NET Authentication, I am currently following a tutorial mentioned HERE. The tutorial is brief (~under 10 mins), but I encountered an error stating Failed to load resource: net::ERR_CONNECTION_REFUSED. Typically, ...

Using TextField with nested Object for onChange Event

I have a question that needs some clarification. Here is the current structure of my object. Within "allgemein," there are currently two variables, but they will increase over time... { id: "Abadi", name: "Abadi", type: "SC", allgemein: { ...