Utilizing JavaScript and Webix to refer to objects in array format

This question revolves around Javascript and JSON, although I'm also open to any solutions using Webix.

Context

Currently, I am utilizing the Webix Javascript UI framework to populate a Datatable with JSON data structured like this:

complexData = {
  "metadata": {
    "itemno": 111222333,
    "groupid": 19,
    "name": "Blah"
  },
  "configs": [
    {
      "id": 1,
      "name": "First",
      "description": "some stuff",
      "value": 222
    },
    {
      "id": 3,
      "name": "Third",
      "description": "Foo",
      "value": 333
    }
  ],
  "system": null
}

The initialization of my Datatable component is done as follows:

webix.ui({
    view:"datatable",
    columns:[
        { id:"id", header:"ID" },
        { id:"name", header:"Name" },        
        { id:"description", header:"Description", fillspace: true },
        { id:"value", header:"Value" }
    ],
    data: complexData.configs
});

Everything functions perfectly in this setup. You can view the output here.

Now, due to some processing requirements later on, I need to slightly alter my data structure. The objects within 'configs' should now be keyed based on their 'id', which can be arbitrary. This means that 'configs' becomes an object of objects instead of just an array:

complexData = {
  "metadata": {
    "itemid": 111222333,
    "groupid": 19,
    "name": "Blah"
  },
  "configs": {
    "1": {
      "id": 1,
      "name": "First",
      "description": "some stuff",
      "value": 222
    },
    "3": {
      "id": 3,
      "name": "Third",
      "description": "Foo",
      "value": 333
    }
    },
  "system": null
}

However, this new structure does not render the rows properly.

You can see the difference here.

Issue

When logging the working result (array) and the failing result (object of objects), the issue becomes apparent:

(2) [{…}, {…}]
  0: {id: 1, name: "First", description: "some stuff", value: 222 }
  1: {id: 3, name: "Third", description: "Foo", value: 333 }
  length: 2
{1: {…}, 2: {…}}
  1: {id: 1, name: "First", description: "some stuff", value: 222 }
  3: {id: 3, name: "Third", description: "Foo", value: 333 }

As per Javascript/Webix, 'complexData.configs' appears as a single object rather than a collection of objects.

Hence, the main question at hand is:

How can 'complexData.configs' be referenced so that Javascript/Webix recognizes it either as an array or a collection?

Alternatively,

Is there a method (like mapping) by which Webix can parse 'complexData.configs' to understand the objects within as datatable rows?

Thank you.

Answer №1

One convenient solution can be found by using the Object.values() method.

const complexData = {
  "configs": {
    "1": {
      "id": 1,
      "name": "First",
      "description": "some stuff",
      "value": 222
    },
    "3": {
      "id": 3,
      "name": "Third",
      "description": "Foo",
      "value": 333
    }
  }
}

console.info(Object.values(complexData.configs))

It's important to note that this feature is not supported in Internet Explorer, but there are various polyfills available for compatibility.

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

How to initialize an enum in Java using Gson

Take a look at this piece of code: public class MyClass { public static enum MyEnum { ENUM_A("This is option A"), ENUM_B("This is option B"); private String description; private MyEnum(String desc) { this.description = desc; } ...

Simple steps to update array key values in PHP

I am attempting to retrieve word counts from HTML documents stored in a specific folder. <?php $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('fulltext/course')); $fulltext_course_files = array(); foreach ($rii as $f) { ...

javascript - Alternate text colors several times within specified color ranges

Seeking assistance with changing the text color multiple times from #627CA9 to #FFFFFF and vice versa. Here's what I've tried: function changeColor(id) { var x = document.getElementById(id); if (document.getElementById(id).style.color = " ...

Getting command line argument parameters in React can be achieved by utilizing the `process.argv`

Is there a way to retrieve command line argument parameters in React? I currently have a React codebase that is utilizing Webpack. When running the following commands - npm run build -- --configuration=dev or npm run build -- --configuration=qa I need t ...

Is it possible to assign functions to each keystroke that does not correspond to a specific keybinding in Angular?

In Angular, there are events tied to keybindings or actions like (focus), (blur), (keydown), and more. You can bind specific keybinds to certain keys as well, such as (keydown.enter), (keydown.alt), etc. Is there a method to trigger an event only when it ...

Encountered a node-pre-gyp installation error while running npm install

As I attempted to follow the React in Action book, I encountered an issue with the project repo - book's project repo Upon running npm install on Ubuntu, I encountered the following error: npm ERR! errno 1 npm ERR! <a href="/cdn-cgi/l/email-prote ...

Create dynamic cells for CSS grid using JavaScript

I have been manually generating grid cells in a specific pattern by copying and adjusting <div> elements. Although this method works, I am interested in creating an algorithm that can automatically generate the desired layout. The left box in the exa ...

What is the best method to send a HTTP request (specifically for scraping) to an Angular2 website?

I am attempting to utilize a node server to extract data from an angular2 application. However, the issue I am encountering is that the response I receive is just the index.js file, which essentially represents the "loading..." section of the webpage. My ...

Is there a way to detect esbuild's build errors and execute a script in response?

Does anyone know how to handle esbuild's build error and trigger a script afterward? I'm integrating it into my workflow with npm, VSCode, and pure JavaScript. I've searched everywhere but haven't found any information on this specific ...

execute a setTimeout function in ReactJs to render a component after a specified amount of time

Is there a way to render a component inside my App.Js after a certain amount of time using setTimeout? I've tried, but nothing seems to be happening... This is my code: function App() { return ( <Router> <GlobalStyle /> ...

What exactly is the function of the NextPage feature in Next.js?

Recently, I began incorporating TypeScript into my Next project. Could someone clarify the purpose of the following code snippets for me? import { NextPage } from 'next'; export const Page: NextPage = () => {} After reviewing the documentation ...

Top method for creating 12 dynamic product pages using AngularJS with customized routing and templates

As someone who is still relatively new to AngularJS, I am looking for advice on how to properly structure the products section of my application. My main product page will display all 12 products with clickable links to individual product pages. Each indi ...

Can you explain the function of MathUtils.euclideanModulo and how it sets itself apart from the traditional modulo operation?

I'm a little puzzled by the euclideanModulo function in threejs's mathutils. I understand the concept of the modulo operator, but the euclideanModulo function seems to work differently. ( ( n % m ) + m ) % m I tried researching the meaning of "E ...

Expecting function to return an undefined response object

My experience with async/await is limited, but I have used these keywords in a function that retrieves or posts data to a MongoDB database. However, it seems like the await keyword does not wait for the promise to be fulfilled and instead returns an undefi ...

JavaScript Variable Naming ConventionEnsuring correct spelling in variable names is essential

While I may not be a JavaScript expert, I have a question about its dynamically typed nature. We are all aware that JavaScript (node.js) is a dynamically typed language where we can easily assign values like: someObject.attr = 123; However, due to the la ...

What is the best way to utilize Link navigation in order to navigate away from a blocked route in the latest version of Next.js,

DISCLAIMER: I raised this issue on the Next.js GitHub repository, but it was closed without recognition. The solution provided did not resolve my problem, leading me to seek help here. The Issue at Hand I have created a demo app (accessible here) on Code ...

Having trouble connecting to the http json service in AngularJS

I recently started learning angularjs and I'm facing an issue with my code. There seems to be a flaw in the code and I'm uncertain about it. From a java perspective, the httpController has a nested function defined inside. Below is the code sn ...

Identify a CSS style or attribute that is being dynamically assigned using JavaScript

While using the Jquery Cycle plugin for my slideshow, I'm looking to trigger an event when a specific slide is active. The Cycle plugin handles this by adjusting the opacity of images within a div in this manner: <div style="position: absolute; to ...

update the componentWillMount() function

I am currently exploring the code found at https://github.com/Spyna/react-store/blob/master/src/createStore.js What adjustments do I need to make in order to align with the deprecated componentWillMount lifecycle method? CreateStore const createStore = ...

Exploring the iteration process of a JavaScript array with Node-API

Currently, I am in the process of developing a Node.js addon utilizing Node-API. My algorithm takes a Javascript array as input, processes it within the addon, and then returns the result. For implementing any logic on the array, I need to iterate through ...