What are the characteristics of an array?

Is there anyone who can assist me with writing a code to count the properties of an array? I have an array and I need to tally up all the complete items that have a value of 1. Below is the array:

[{
    "order_id": "336566",
    "customer_name": "joel",
    "customer_surname": "kabeya",
    "total_items": "0",
    "completed_items": "0",
    "percent_complete": 1,
    "datetime_received": "2019-01-21 15:00:27",
    "delivery_date": "2019-01-24",
    "delivery_from": "10:30",
    "delivery_to": "12:00",
    "completed": "1",
    "shopper": "joel"
}, {
    "order_id": "335719",
    "customer_name": "joel",
    "customer_surname": "kabeya",
    "total_items": "0",
    "completed_items": "0",
    "percent_complete": 1,
    "datetime_received": "2018-12-24 13:46:27",
    "delivery_date": "2018-12-30",
    "delivery_from": "10:30",
    "delivery_to": "12:00",
    "completed": "1",
    "shopper": "joel"
}, {
    "order_id": "336531",
    "customer_name": "Tay",
    "customer_surname": "Z",
    "total_items": "0",
    "completed_items": "0",
    "percent_complete": 1,
    "datetime_received": "2019-01-11 08:42:27",
    "delivery_date": "2019-01-17",
    "delivery_from": "10:30",
    "delivery_to": "12:00",
    "completed": "1",
    "shopper": "joel"
}, {
    "order_id": "336545",
    "customer_name": "joel",
    "customer_surname": "kabeya",
    "total_items": "0",
    "completed_items": "0",
    "percent_complete": 1,
    "datetime_received": "2019-01-17 19:00:27",
    "delivery_date": "2019-01-18",
    "delivery_from": "11:00",
    "delivery_to": "12:00",
    "completed": "0",
    "shopper": "joel"
}, {
    "order_id": "241918",
    "customer_name": "Marietjie",
    "customer_surname": "Short",
    "total_items": "44",
    "completed_items": "44",
    "percent_complete": 1,
    "datetime_received": "2018-07-25 15:18:25",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": "Tay"
}, {
    "order_id": "281774",
    "customer_name": "Ashleigh",
    "customer_surname": "Hodge",
    "total_items": "16",
    "completed_items": "0",
    "percent_complete": 0,
    "datetime_received": "2018-10-04 15:59:19",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": null
}]

Answer №1

Utilize the Array.filter() method along with the length property of Array. Keep in mind that the condition for completion is when the completed property has a value of "1". Therefore, filter the data based on this condition and then calculate the remaining items.

const data = [{
    completed: "1"
  },
  {
    completed: "1"
  },
  {
    completed: "0"
  }
];

const remainingCount = data.filter(item => item.completed === "1").length;

console.log(remainingCount);

Answer №2

Utilize a foreach loop for iteration and addition. Additionally, the filter method can also be implemented.

//foreach

The foreach loop iterates through each element, checking if the object has the completed property set to 1. If it does, the counter is incremented and returned. Otherwise, the counter remains the same.

//filter

Similarly, the filter function loops through each element and checks if they meet the condition (completed==1). If true, the element is added to a new array, the length of which is then printed. If false, the element is excluded from the array.

var a=[{
    "order_id": "336566",
    "customer_name": "joel",
    "customer_surname": "kabeya",
    "total_items": "0",
    "completed_items": "0",
    "percent_complete": 1,
    "datetime_received": "2019-01-21 15:00:27",
    "delivery_date": "2019-01-24",
    "delivery_from": "10:30",
    "delivery_to": "12:00",
    "completed": "1",
    "shopper": "joel"
}, {
    "order_id": "335719",
    "customer_name": "joel",
    "customer_surname": "kabeya",
    "total_items": "0",
    "completed_items": "0",
    "percent_complete": 1,
    "datetime_received": "2018-12-24 13:46:27",
    "delivery_date": "2018-12-30",
    "delivery_from": "10:30",
    "delivery_to": "12:00",
    "completed": "1",
    "shopper": "joel"
}, {
    "order_id": "336531",
    "customer_name": "Tay",
    "customer_surname": "Z",
    "total_items": "0",
    "completed_items": "0",
    "percent_complete": 1,
    "datetime_received": "2019-01-11 08:42:27",
    "delivery_date": "2019-01-17",
    "delivery_from": "10:30",
    "delivery_to": "12:00",
    "completed": "1",
    "shopper": "joel"
}, {
    "order_id": "336545",
    "customer_name": "joel",
    "customer_surname": "kabeya",
    "total_items": "0",
    "completed_items": "0",
    "percent_complete": 1,
    "datetime_received": "2019-01-17 19:00:27",
    "delivery_date": "2019-01-18",
    "delivery_from": "11:00",
    "delivery_to": "12:00",
    "completed": "0",
    "shopper": "joel"
}, {
    "order_id": "241918",
    "customer_name": "Marietjie",
    "customer_surname": "Short",
    "total_items": "44",
    "completed_items": "44",
    "percent_complete": 1,
    "datetime_received": "2018-07-25 15:18:25",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": "Tay"
}, {
    "order_id": "281774",
    "customer_name": "Ashleigh",
    "customer_surname": "Hodge",
    "total_items": "16",
    "completed_items": "0",
    "percent_complete": 0,
    "datetime_received": "2018-10-04 15:59:19",
    "delivery_date": "2018-10-29",
    "delivery_from": "12:00",
    "delivery_to": "13:00",
    "completed": "0",
    "shopper": null
}]
var count=0;
a.forEach((e)=>e.completed=="1"?count++:count)
console.log(count);
//filter
console.log(a.filter((e)=>e.completed=="1"?true:false).length)

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

What exactly is the functionality of the third parameter (usually next()) behind the scenes in ExpressJS once it is hidden behind the abstraction layer?

Consider this scenario: in the following two code snippets, how is the next() function used as a parameter and how does it facilitate the automatic transition to the next middleware function? What is the underlying mechanism that enables this abstraction? ...

Incorporating the Acts_as_votable gem alongside Angularjs for interactive voting functionality

I'm trying to figure out how to implement Acts_as_Votable in an Angular template for a car voting system. Despite my efforts, I can't seem to display the vote count when rendering the list in Angular. I think I may need to establish some kind of ...

The process of retrieving request data from axios.get and storing it in the Redux store

Recently delving into Redux, I'm curious about how to retrieve request data from a GET method. Upon mounting the component, you can use axios to send a GET request to '/api/v3/products', passing in parameters like pageNumber and pageSize. ...

"Is there a way to adjust the range slider to display currency instead of

I stumbled upon this amazing slider on codepen. Can someone guide me on how to adjust it to display a range from €500 to €6000 while keeping the vibrant red background? I've attempted various solutions like: <input id = "range" type = "range ...

There is a problem with my module where multiple files that require it are overriding its variables

Currently, I am working on developing a mongo connection pool factory that is capable of checking if a connection to mongo already exists. If a connection exists, it will return that connection. However, if there is no existing connection, it will create a ...

Clarifying the confusion surrounding AngularJS $q, promises, and assignments

Curious about a particular behavior I'm witnessing. Unsure if there's a misunderstanding on my part regarding promises, JavaScript, or Angular. Here's what's happening (I've prepared a plnkr to demonstrate - http://plnkr.co/edit/ZK ...

Update your content dynamically by refreshing it with JQuery/AJAX following the usage of an MVC partial view

Implementing the following JQuery/AJAX function involves calling a partial view when a selection is made in a combobox labeled "ReportedIssue" that resides within the same partial view. The div containing the table is named "tableContent". <script type ...

Executing a JavaScript function when a selection is made from a dropdown menu

I'm trying to create a searchable dropdown in the code below. When a value is selected from the dropdown, it should call a JavaScript function. However, I am facing issues with the code not working as expected. Can someone please assist me in resolvin ...

Creating a Piechart in Kendo UI that is bound to hierarchal remote data

I am facing an issue with binding remote data to a pie chart while managing a grid with dropdown sorting options. The grid is working fine, but I am unable to display the hierarchical data on the pie chart as categories. <!DOCTYPE html> <html> ...

Set up an event listener for a specific class within the cells of a table

After spending the last couple of days immersed in various web development resources, I find myself stuck on a particular issue. As someone new to this field, the learning curve is quite steep... Let's take a look at a single row in my project: < ...

Javascript function to deselect all items

One of my functions is designed to reset all checkbox values and then trigger an AJAX request. However, there are instances when the function initiates before the checkboxes have been unchecked. function clear() { $("#a").prop("checked", false); $("#b ...

Tips for customizing the `src/app/layout.tsx` file in Next.js 13

I am looking to customize the layout for my /admin route and its child routes (including /admin/*). How can I modify the main layout only for the /admin/* routes? For example, I want the / and /profile routes to use the layout defined in src/app/layout.ts ...

Generate Array of Consecutive Dates using JavaScript

My array contains the following values (for example): [ 1367848800000: true, 1367935200000: true, 1368021600000: true, 1368108000000: true, 1368194400000: true, 1368367200000: true, 1368540000000: true, 1 ...

Obtain the registration ID for Android to enable push notifications by utilizing PushSharp

I am currently utilizing the PushSharp library and have come across deviceToken in the sample code provided on this link. Could someone kindly assist me on how to obtain this deviceToken? The PushSharp sample code does not clearly explain this. apnsBrok ...

What is the best way to apply fading effects to three divs containing additional divs?

Looking at this HTML and CSS code: HTML <DIV class="newsPic"></DIV> <DIV class="newsPicTwo"></DIV> <DIV class="newsPicThree"></DIV> CSS .newsPic { width: 500px; height: 200px; } .newsPicTwo { width: 500px; height: 2 ...

Utilizing AngularJS and RequireJS to incorporate a controller into the view

I am facing an issue while trying to add an Angular controller to my HTML view. Typically, in Angular, this can be done using: ng-controller="<controller>". However, due to my use of RequireJS, I have had to implement it differently. I need to includ ...

Refreshing various innerHTML elements using a universal function

I'm attempting to consolidate several similar functions into one, but I'm encountering some challenges. Below is an example of one of the original functions that is called by a button press: function ADD_ONE(Variable_Name){ Variable_Name += ...

What benefits and drawbacks come with setting up JS libraries in resource files compared to package.json?

Configurations for JavaScript libraries such as Babel, Nyc, Eslint, and many others can be specified in resource files or within the package.json. For example, Babel can be set up in a .babelrc file or through a babel entry in the package.json. What are ...

Validating Laravel emails with JavaScript blur and utilizing AJAX and jQuery

Looking for a way to validate a form in Laravel without hitting the submit button? I've tried some code, but only the email format validation seems to be working. Any tips on what I should do next? I'm new to Ajax. PS: When I enter a valid email ...

Extract the text content from an HTML file while ignoring the tags to get the substring

Hello, I am a newcomer to the world of Web Development. I have an HTML document that serves as my resume, formatted in HTML. For example: html <p>Mobile: 12345678891 E-mail: <a href="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...