How should a JavaScript object be properly formatted?

Recently, I created a project using ng-repeat with AngularJS 1.x, and it was smooth sailing.

JavaScript:

var app = angular.module('myModule', []);

app.controller('myController', function() {
  this.dishes = [
    {
      'name': 'CD',
      'drink': 'wine',
      'color': 'red'
    },
    {
      name: 'vagetable',
      drink: 'water',
      color: 'blue'
    },
    {
      'name': 'meat',
      'drink': 'coffee',
      'color': 'brown'
    }
  ];
});

HTML:

<html lang="en" ng-app="myModule">

<head></head>

<body ng-controller="myController as myCtrl">
  <div> {{myController.dishes}} </div>
  <ul>
    <li ng-repeat="dish in myCtrl.dishes">
      <p> {{dish.name}} is my name.</p>
      <p> {{dish.drink}} is something you can drink.</p>
      <p> {{dish.color}} is the color I wear.</p>
      <hr>
    </li>
    <p> {{myCtrl.theModelContent}}</p>
    <input type='text' ng-model="myCtrl.theModelContent">
  </ul>
</body>

</html>

I observed that both 'name': 'CD' and name: 'vegetable' work without any issues. Curious to know what sets them apart?

Answer №1

When it comes to JSON, everything is treated as a string in this wire format. This is why we commonly use JSON.stringify for JavaScript object literals. On the other hand, JavaScript object literals can have keys that are either strings or non-strings. In summary, while having a string key in a JavaScript object literal is not necessary, it is a requirement in the JSON-formatted version of the same object.

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 there a messaging app that provides real-time updates for when messages are posted?

I am in the process of developing a messaging application. User1 sends out a message, and I want to display how long ago this message was posted to other users - for example, "Posted 9 min. ago", similar to what we see on sites like SO or Facebook. To ach ...

Simple Mobile-Friendly Tabs - mobile tabs are always visible

My JavaScript skills are not the best. I have been using Easy Responsive tabs and encountered an issue on the mobile version where clicking does not hide content, but only removes the "d_active" class and adds it to another element. I believe that if the ...

Retrieve the element that is currently being hovered over within the same nested selector

I am facing a challenge in selecting the currently hovered element with a nested selector. The ".frmElement" class is used as the selector. When I hover over the ".frmElement" element at a certain level, all the previous selector elements display the hover ...

Utilizing jQuery to dynamically add results and prevent duplicate entries in will_paginate

I am currently using will_paginate to easily manage the comments pagination in my Rails 3 application, and so far it's been working flawlessly. At the moment, I have set up the display to show 10 comments per page. However, whenever I add a new comme ...

Testing an AngularJS Service using Unit Testing

I've encountered an issue while unit testing a basic User Service that has two exposed functions. Below are the test and service provided. Every time I run the tests, I keep encountering the error message below: PhantomJS 2.1.1 (Mac OS X 0.0.0) User ...

Enhancing ranking capabilities with Firebase queried data

Currently working on an app using Firebase and facing some concerns about database scalability. I have decentralized my data, but struggling to efficiently rank posts based on the number of likes they receive. For instance, displaying the top 10 most liked ...

Is there a way to determine the app bar height within a React Material UI application?

I'm trying to create a full-screen image for the opening of my website using React and Material UI. Here's a snippet of my JSX code with the default Material UI theme: <AppBar> //code in between </AppBar> <Container sx={{margin: ...

Node.js encountered an error due to a self-signed certificate within the certificate chain

I'm encountering an issue with client-side HTTPS requests. An example snippet is as follows: var fs = require('fs'); var https = require('https'); var options = { hostname: 'example.com', port: 443, path: & ...

A compilation of approved classes for the quill toolbar

The instructions provided in the documentation for the quill module indicate that you can manually create a toolbar using HTML, and then pass the corresponding DOM element or selector to Quill. This will result in the ql-toolbar class being added to the to ...

Tips for including a JSON file within the utils directory of a Node.js project

I have a JavaScript file located in the utils folder of my Node.js project. This JS file is responsible for retrieving data from a database. However, at the moment, I only have mock data stored in a local JSON file. Now, I need to figure out how to load th ...

Is there a simple method in JavaScript to combine, structure, and join numerous multi-dimensional arrays in a specific manner (from right to left)?

Looking for a simple solution to merge, flatten, and concatenate multiple multi-dimensional arrays in JavaScript in a specific manner (from right to left) # Example [['.class1', '.class2'], ['.class3', ['.class4', & ...

Add items to a separate array only if the material UI checkbox is selected

Exploring the world of React, I decided to create a simple todo app using React JS and Material UI. With separate components for user input (TodoInput.js) and rendering individual todos with checkboxes (TodoCards.js), I aim to display the total number of c ...

The JS copy function is successful when operating on a single element, but it encounters issues when attempting to run on multiple elements, only copying

My code includes a copy function that copies the data from a textarea to the clipboard when a button is clicked. The functionality works perfectly for the first textarea, but for subsequent textareas, the buttons do not perform the copy action. Check out ...

Using Angular and Jade to pass an array from a controller to script tags

I am trying to figure out how to access an array in my controller and display it accurately within the <script> tags in my jade template. For instance: Controller.js $scope.myArray = ["item1","item2"]; Within my index.jade: script. var clien ...

Is there a way for me to personalize the background of the mui-material datagrid header?

I am looking to update the background design of Mui Datagrid from this image to this image However, I am encountering an issue where the background color does not fully cover the header. I have attempted using "cellClassName:" in the columns but it has n ...

Error: Unable to locate module: Material-UI - Please check the path and try again

Encountering this error: Error message: Failed to compile ./node_modules/@material-ui/core/Modal/Modal.js Module not found: Can't resolve '@babel/runtime/helpers/builtin/assertThisInitialized' in 'C:\Users\rifat\ ...

Leveraging the node CLI tool as a library for trimming MP3 files (trimp3

I recently came across a fantastic library that I am interested in using for my nodejs project: https://github.com/kyr0/trimp3 The only issue is that it functions as a cli tool, and I would like to integrate it seamlessly into my codebase as a library. D ...

What is the best way to activate a function within an npm package in a Vue application?

I'm just starting out with Vuejs and I've recently installed the vue-countup-v2 npm package. I successfully imported it into my Vue component and noticed that it works perfectly when the page loads. However, I am interested in triggering the Coun ...

Is there a way to prevent vertical scrolling during left swipes in Ionic?

I am in the process of creating an Ionic mobile application, where the main view consists of a vertical list of cards. I would like each card to be "swipable" in a similar fashion to Google Now cards. I have begun implementing this functionality: $scope. ...

Eliminating Non-Breaking Spaces with the Click of a Button

Having some difficulty with the following Javascript code. I'm not very skilled in writing Javascript, so any assistance on adjusting it to replace any &nbsp; with a regular space would be greatly appreciated. Thank you function copyToClipboard( ...