Attempting to replicate my console.log outputs

I have a variable that contains 4 objects which I can view when logging it out in the console. However, I am struggling to create the desired structure for this return variable (I want to sort it but need to first recreate the structure).

When I check the original variable in the console, it shows: [Object, Object, Object, Object]

But with the created variable, I only get: [Object]

Below is my attempt to re-create it:

this.obj = [{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                   {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
           color:'blue'}],
          [{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                   {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
           color:'green'}],
          [{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                   {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
           color:'red'}],
          [{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                   {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
           color:'yellow'}]
          ;

The first objects in both original and created are the same, but why can't it recognize the other three objects? If I add two additional square brackets around my code, all four are recognized, but then they are changed into arrays instead of objects...

Answer №1

Your code appears to be malfunctioning, but if you organize it in a clear and coherent manner, the issue should become evident:

var obj = [
    {
        items: [{
            code: 'bravo', color: 'blue', date: '2017-01-01', pos: 'up'
        }, {
            code: 'alpha', color: 'blue', date: '2017-01-02', pos: 'down'
        }],
        color: 'blue'
    }] // <=== the array ends here, but now you have this:
    , [{ items: [{ ...

The compiler then proceeds as follows:

var obj = [
    {
        items: [{
                code: 'bravo', color: 'blue', date: '2017-01-01', pos: 'up'
            }, {
                code: 'alpha', color: 'blue', date: '2017-01-02', pos: 'down'
            }],
        color: 'blue'
    }
], _a = (void 0)[0], _b = ....

Upon closer inspection, you can observe that it introduces another variable (_a) to accommodate the additional data.

Answer №2

Here's the solution that worked successfully:

this.data = [{0:{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                         {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
                 color:'blue'}},
                {1:{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                         {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
                 color:'green'}},
                {2:{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                         {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
                 color:'red'}},
                {3:{items:[{code:'bravo',color:'blue',date:'2017-01-01',pos:'up'},
                         {code:'alpha',color:'blue',date:'2017-01-02',pos:'down'}],
                 color:'yellow'}}]
                ;
}

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

Determine if the webpage is the sole tab open in the current window

How can I determine if the current web page tab is the only one open in the window? Despite searching on Google for about 20 minutes, I couldn't find any relevant information. I would like to achieve this without relying on add-ons or plugins, but if ...

Receiving resource in PHP leads to the error message 'Unable to treat object of type stdClass as an array'

I'm experimenting with a new method for storing string resources in PHP, but I'm encountering some difficulties. Specifically, I'm having trouble understanding how the __get function interacts with arrays and objects. Error Message: "Fatal ...

Redirect to a randomly selected website from an array of links after a certain period of time

Here is some code snippet that I'm working with: jQuery(document).ready(function() { var textArray = [ 'www.yahoo.com', 'www.wikipedia.org' ]; var randomNumber = Math.floor(Math.random()*textArray.length); link.se ...

AngularJS: Issue with ngChange not being triggered from directive

A Summary of the Issue at Hand I've created a directive that dynamically displays a list of checkboxes. The directive has a parameter named options, which should be an array structured like this in order for the checkboxes to display correctly: var ...

Preventing Div items from rearranging during size transitions using toggleClass

When I click on an element with the ID "id", I use toggleClass to resize a div from 10px to 500px in width. This is done partly to show/hide content. However, the issue arises when the transition occurs and the contents of the div start rearranging, overfl ...

(MUI Textfield) Error: Unable to access properties of undefined (specifically 'focus')

I have been working with the useRef hook in my Material Ui Textfield and have encountered the error Uncaught TypeError: Cannot read properties of undefined (reading 'focus'). I am unsure how to resolve this issue. Here is my current code snippet: ...

Storing HTML elements in a database using jQuery's html() method

I have encountered a dilemma that has left me stumped after extensive online research. I have a dynamically generated webpage and need to save the entire appended body of the page to a database using PHP and MySQL. My current approach involves using the fo ...

Issue with saving Twitter stream data to MongoDB using Mongoose not being properly handled

My goal was to store data from the Twitter Streaming API into MongoDB using Mongoose. However, I'm facing an issue where only a single document is saved out of the continuous stream of data when I run the code: var mongoose = require('mongoose&a ...

The React state may not accurately reflect changes when removing an element from an array

I created a React component that is designed to remove the last element of an array when a button is clicked. However, even though I am correctly logging the value in the console, my React state is not updating as expected (as shown in React Developer Tool ...

Is the TypeScript compiler neglecting the tsconfig.json file?

I am new to TypeScript and currently exploring how to set it up in WebStorm. One of the first steps I took was creating a tsconfig.json file in the main directory of my project and updating the built-in TypeScript compiler to version 1.6.2. However, despit ...

Exporting State From Another ReactJS Module: A Step-by-Step Guide

A new project is underway with 3 students who are diving into the world of React for the first time. To make our work more efficient, I suggested dividing the code so that each student could work on a different aspect simultaneously. However, when I attemp ...

utilizing a decimal point that is not in accordance with my cultural norms

I have encountered a bug where, despite setting the language of Windows 10 to Italian and adjusting the decimal separator in the control panel, our website is unable to display numbers with it. Is there a way to instruct devextreme to use the comma speci ...

Error message "ag-grid: Unable to perform the key.forEach function in the console when resizing columns"

Within the application I am working on, I have implemented the ag-grid view. To address the issue related to the last empty pseudo column, I decided to resize the last displayed column using the 'autoSizeColumns' method of ag-grid. While this sol ...

Click on the entire Material-UI formControlLabel to select it, not just the text

I have recently started working with the material UI. Currently, I am dealing with a form that looks like this: <FormControl variant="outlined" className={css.formControl} margin="dense" key={"abc_" + index} > <FormControlLabel cont ...

What is the best way to separate multiline cells into arrays in Google Sheets?

Keeping track of the comics I've read can be a bit challenging, especially when some titles have issue numbers spread across multiple lines. Take for example the Avengers series that began in 2016 - here are the issue numbers listed in one cell: #1-1 ...

The findOneAndUpdate function in MongoDB is adding a new record into the database

Whenever I make an update API call, I just need to update the serviceActiveFlag status. After the update API call, a new document with an empty vehicle array is created, as shown below: _id:59c76073c11d3929148f500f vehicle:Array _v:0 The ID field will ov ...

Array of Angular FormGroup

I am facing an issue with setting data to the form array in my project. The scenario is that when filling out user details, some users have multiple addresses. Here is User.ts export interface User { id?: number; name: string; age: number; address ...

Tips for creating an illustration in Vue.js

As I attempt to create an image using canvas, my browser throws this error at me: Uncaught TypeError: Cannot read property 'drawImage' of undefined at Image.img.onload (test.js:23) To troubleshoot, I added some console.log() messages and here ...

Combining strings and variables in Vue.js with the use of bootstrap-vue syntax

Hey, I'm facing a little syntax hiccup with '="collapse-{{ product.id }}"' in both the b-button and b-collapse. Any ideas on how to properly structure this? Just looking to set up a unique ID that connects the button to the collaps ...

Steps for selectively targeting and updating a group of properties in a TypeScript class

Is there a way to consolidate this code into one function that can handle all the tasks below? I'm adding more text here to meet the requirements and hoping for a solution. Thank you! TypeScript is an amazing language that differs slightly from JavaS ...