Is there a way to sort through objects in a JSON file using two shared values? Specifically, I'm looking to filter the JSON objects based on both common x and y values

Given a JSON file, I am looking to group objects based on common x and y values. Essentially, I want to group together objects that share the same x and y properties. Here is an example of the JSON data:

let data = [{
"x": "0",
"y": "0",
"k": "0"
},
{
"x": "0",
"y": "0",
"k": "1"
},
{
"x": "1",
"y": "2",
"k": "0"
},
{
"x": "1",
"y": "2",
"k": "5"
},
{
"x": "2",
"y": "2",
"k": "10"
},
{
"x": "1",
"y": "2",
"k": "12"
}
]

The desired result would be as follows:

result = [
[{
"x": "0",
"y": "0",
"k": "0"
},
{
"x": "0",
"y": "0",
"k": "1"
}],
[
{
"x": "1",
"y": "2",
"k": "0"
},
{
"x": "1",
"y": "2",
"k": "5"
},
{
"x": "1",
"y": "2",
"k": "12"
}
],
[{
"x": "2",
"y": "2",
"k": "10"
}]
]

I only need to separate the objects with common x and y, how can I go about solving this issue?

Answer №1

You might want to consider implementing something along these lines:

// Assume that a specific character like "-" does not appear in x / y
const groupA = {};
data.forEach((item) => {
  const key = `${item.x}-${item.y}`;
  groupA[key] = [];
  groupA[key].push(item);
});

const resultA = Object.values(groupA);

// If there is no guaranteed separator
const groupB = {};
data.forEach((item) => {
  groupB[item.x] = groupB[item.x] || {};
  groupB[item.x][item.y] = groupB[item.x][item.y] || [];
  groupB[item.x][item.y].push(item);
});

const resultB = Object.values(groupB).map(item => Object.values(item)).flat();

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

Performing an Ajax POST request using jQuery

I am currently working on modifying my code to use POST instead of GET to send variables to a PHP page. The current code sends data via GET and receives it in JSON format. What changes should I make in order to pass the variables to process_parts.php usi ...

Combining two or more arrays containing similar values into a single array

Within my array, there are 100 subarrays, each containing 3160 values of an object with two attributes: a sequence number (which only appears if it is present as 1), and another value of either 0 or 1. My goal is to merge all 100 arrays into one single ar ...

Unable to release my json object in C due to an assertion failure

Here is a JSON object I am working with: { "heartbeat": { "status": 0, "tempID": 1210254506, "firmware": "obu-hmi-commsignia", "sync": 1, "upgrade": 0, "lo ...

Why is it that a specific variable is only undefined in one specific location within the entire component?

import React from 'react'; import { Formik, Form } from "formik"; import { InputField } from "./formui/InputField"; import { applyGharwapasi } from "../../appollo/applyGharwapasi/applyGharwapasi"; import { useMutatio ...

What is the best way to verify multiple email addresses simultaneously?

Is there a method to validate multiple email addresses entered by users in a textarea? My current approach involves using ng-pattern for validation. ...

Include a lower border on the webview that's being shown

Currently, the webview I'm working with has horizontal scrolling similar to a book layout for displaying HTML content. To achieve this effect, I am utilizing the scroll function. My inquiry revolves around implementing a bottom border on each page usi ...

Create a line connecting two divs using jQuery's DOM manipulation capabilities

Looking to connect two divs with a straight line, I stumbled upon jQuery DOM line, which appears to offer a more streamlined solution compared to jsPlump. I attempted to incorporate it into my code, but unfortunately, it's not working as expected. Be ...

jquery method for clearing input field values

I am facing an issue with the form where the company name field automatically loads a value, even though it is a required field. I simply want to clear the previous loaded value and allow the user to input their information. For example, if I previously e ...

Transform a two-dimensional array into a JSON object within the Laravel framework

Recently, I've been working on converting a two-dimensional array into a JSON object. Here is the current output produced by my controller: [ [ { "id": 1, "title": "V1", &q ...

Is there a way to determine if a line has been automatically wrapped within a textarea element?

Is it possible to detect when text is wrapped onto the next line in a textarea after becoming wider than the area? ...

Is JavaScript not having an impact on your HTML code?

I am attempting to create a dynamic number change when the user interacts with the "range-slider" element, but the number is not updating as expected. Below is the code I have written: var rangeSlider = function() { var slider = $(".range-slider"), ...

Can Chrome Support Bookmarklets?

While attempting to craft a bookmarklet in Chrome using the console, I encountered the following error: Refused to load the script 'https://code.jquery.com/jquery-1.6.1.min.js' because it violates the following Content Security Policy directive: ...

Exploring TypeScript's type discrimination with objects

When working with TypeScript, type discrimination is a powerful concept: https://www.typescriptlang.org/play#example/discriminate-types But is it possible to achieve something like this? type A = {id: "a", value: boolean}; type B = {id: "b ...

Tips on entering a text field that automatically fills in using Python Selenium

One of the challenges I am facing on my website is an address input text field that gets automatically populated using javascript. Unlike a drop-down field where you can select values or a standard text field where you can manually type in information, thi ...

Tips for resizing a 100% div without displaying vertical scrollbars in the browser

I am facing an issue with a table that has three rows. I have a main #container div with a height of 100%, and inside it is a table with 3 rows. The first and last row contain fixed size content. However, in the second row, there is a #content div with a h ...

AngularJS: encountering an issue with undefined vm variable while implementing controllerAs syntax within a directive

I am having trouble accessing the vm.screensize property in the relevant controller. The error message states that vm is not defined. Below you can find the directive and controller code snippets. angular.module('app.ain.directives') .direct ...

Modifying the content of JSON key values

In my application, I am using MongoDB, Express, and MongoDB. When receiving data on the server side, it comes in the form of an array of objects (JSON) named upProbations. For example, let's say I find information about Max: [ { Name ...

Incapable of stacking one canvas on top of another

I'm new to javascript and looking for help with positioning a canvas element behind another. The code I have may be a bit messy, so any assistance is greatly appreciated. My goal is to have the canvas named "myCanvas" appear behind "coinAnimation". Th ...

Tips for customizing the appearance of a child component using the parent component in Vue.js

I am working on creating a layout using Vuejs and flexbox that includes elements like 'header, sidebar, main-content, sidebar, footer'. I have separated each part of the layout into individual .vue files, such as header.vue and sidebar.vue. In ...

Using Angular to link Google Places API responses to a form: a guide on merging two different length objects with a shared key

I'm struggling with a key concept here regarding displaying the results of a places autocomplete query. I want to replace the types[0] name with more familiar terms such as suburb or city instead of sublocality_level_1 or administrative_area_level_1 ...