What is the method for retrieving a specific item within an array object using its index number?

How can I access the object '2016-11-12' inside the $scope.list and display the Order and Balance values?

List:

$scope.list = {
  ItemCode:'item1',
  '2016-11-12':[{Order:'1',Balanace:'2'}],
  '2016-11-15':[{Order:'',Balanace:''}]
}

I attempted to use indexOf but it didn't work as expected.

$scope.list[0].indexof(1) 

Any suggestions or ideas would be highly appreciated!

Answer №1

If you need to retrieve the Order value for a specific date:

$scope.list['2016-11-12'][0]['Order']

To retrieve the Balance value for a particular date (just an FYI, you misspelled balance):

$scope.list['2016-11-12'][0]['Balance']

Answer №2

To access the value using bracket notation:

$scope.list['2016-11-12'];

Here is an example:

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

app.controller("testCtrl", function ($scope) {
  $scope.list = {
  ItemCode:'item1',
  '2016-11-12':[{Order:'1',Balanace:'2'}],
  '2016-11-15':[{Order:' ',Balanace:' '}]
};
  
  $scope.test = $scope.list['2016-11-12'];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="TestApp">
  <div ng-controller="testCtrl">
    {{test}}
  </div>
</div>

Answer №3

The variable $scope.list is not actually an array, but an object containing arrays. The '2016-11-12' data is stored as a field within one of the arrays, which in turn contains objects. To make your object more versatile, consider grouping the objects within an array inside the main item object and accessing them by index.

$scope.list = {
ItemCode:'item1',
ThingsArray:[
 {Date:'2016-11-12', Order:'1',Balanace:'2'},
 {Date:'2016-11-15', Order:' ',Balanace:' '}
 ]
}

After this restructuring, you can refer to specific objects using $scope.list.ThingsArray[0]. Note that the dates have been moved into each individual object for easier retrieval.

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

The lower section of the scrollbar is not visible

Whenever the vertical scroll bar appears on my website, the bottom half of it seems to be missing. For a live demonstration, you can visit the site HERE (navigate to the "FURTHER READING" tab). HTML: <!DOCTYPE html> <html lang="en"> <h ...

Obtaining Results from PHP for JSON/AJAX Handling

A colleague shared with me this JavaScript code snippet that could be useful: $.ajax({ type: "POST", url: "../CheckPerson.php", data: "{'lastName':'" + _lname + "','firstName':'" + _fname + "','midd ...

New feature in jQuery inputmask enables placeholder text to be retained

I have integrated the inputmask feature from https://github.com/RobinHerbots/jquery.inputmask in my project, and I am applying the mask to all textboxes with the class "date". However, I am encountering a problem where if the user leaves one or more letter ...

How to access the ID value from the URL within a different component that was passed as a prop in

I have a scenario where I am building a reusable component with two child components in the following flow: Child Component 1 -> Parent Component -> Super Parent Main Component In the child component, I define a prop called 'url' which is ...

Implementing Ng-debounce selectively for functions within an AngularJS application

I am looking to execute specific functions at different time intervals when there is a change in input. The current setup is as follows: <input type="text" name="title" ng-model="title" placeholder="Search..." ng-model-options="{ updateOn: 'defaul ...

Safari users encountering invalid dates generated by Moment Js library

Can anyone explain why I am seeing "invalid date" in Safari, but it works fine in other browsers? moment('01/01/2023 11:44:00.000 AM').tz(time_zone, true).format('hh:mm:ss:SS A z') chrome https://i.sstatic.net/PeFBp.png safari https ...

approach for extracting values from nested objects using specified key

There are objects in my possession that contain various nested objects: let obj = { nestedObject: { key: value } } or let obj2 = { nestedObject2: { nestedObject3: { key2: value2 } } } and so on. Retrieving the values from these objects i ...

Issue: Ajax is not defined

When attempting to call a URL in the background using the following code within a script tag: var request = new Ajax.Request(logoffURL, {method : 'post'}); I encountered a script error stating Ajax is undefined. Is it necessary to include any ...

What's the best way to display my slider exclusively on the homepage?

Hello, I am experiencing an issue with my slider. I would like to display the slider only on the home page and not on any other sub-pages. Is there a way to achieve this? Below is the code that I am using: <script> $( ...

Forcing the Json Serializer to output a specific datetime format of (yyyy-mm-ddThh:mm:ss.msmsmsZ)

Creating a new instance of MyClass using the following code snippet: MyClass theSession = new MyClass() { accountId = 12345, timeStamp = DateTime.Now, userType = "theUserType" }; Serializing the data using JavaScriptSerializer: System.Web.Sc ...

Unexpected error in log4javascript on IE8: Expected function not found

I'm attempting to redirect console calls to the log4javascript library. Essentially, any usage of console.log should trigger log.info, with log being an instance of Log4javascript. However, when log.info is invoked, I encounter a "Fonction attendue" ...

Updating Variable Values in PHP

Currently, I am working on a project about online shopping using PHP. However, I have encountered an issue regarding changing the currency value. I need to convert the currency to another based on the exchange rate provided. <select onchange=""> ...

Bootstrap relies on jQuery for its JavaScript functionality, so jQuery must be loaded before using Bootstrap's JavaScript

I encountered an issue while trying to load the Bootstrap library, consistently receiving this error message: Uncaught Error: Bootstrap's JavaScript requires jQuery Even though I have ensured that jQuery is loaded before attaching the Bootstrap li ...

I prefer to avoid generating the document structure while parsing with JSOUP

Utilizing the Jsoup API to parse a section of HTML using the Jsoup.parse() method. However, during parsing, it includes the document structure in the HTML content. For Instance: <p><a href="some link">some link data</a> Some paragraph c ...

The versatility of reusable Backbone components

As I search for the best way to ensure the reusability of Backbone views, I have come across various solutions but am unsure which one would best meet my needs. My goal is to create multiple widgets populated with real-time data and I require a base compon ...

JavaScript property counterparts

Recently, I've been working on creating alias's for a specific property in my code. var default_commands = {} default_commands['foo'] = "bar"; My goal is to create multiple aliases for the key 'foo' in the object. For examp ...

Guide to sending an Array of objects in the request body using RestAssured in Java

Here is the Request I am working with: [ { "userId": "value1" },{ "userId": "value2" } ] I attempted to create a POJO class and construct the request, as well as using an ArrayList, but there ...

Can AngularJS be classified as Functional Reactive Programming?

Each time I come across the term Functional Reactive Programming, it becomes apparent that my grasp of its meaning is elusive. After revisiting this thought-provoking article about Functional Reactive Programming, I convince myself that I understand it onl ...

Controller is not cooperating with Artisan migration

I have a database of contacts that is populated through an API call to /users endpoint. It successfully retrieves all user data and displays it in a table. Now, I want the user to be able to click a button and receive an updated contact list with unique v ...

Having issues with Cypress testing of Material-UI datepicker on Github actions

Encountering an unusual issue while running Cypress tests in a GitHub action environment. The MUI datepicker is stuck in readonly mode, preventing any input of dates (works fine in other setups). Error displayed by Cypress CypressError: Timed out retryin ...