javascript The array is showing up as blank

I am trying to return the entire object where linkable is set to true. Even though I am pushing the objects, the this.releaseDescriptionDatesArray remains empty. It seems like my logic is correct, so not sure why the array is not populating.

for (const item of this.datesArray) {
          for (const attribute in item) {
            if (attribute === 'linkable' && item[attribute] === true) {
              this.releaseDescriptionDatesArray.push(item); // data is being pushed 

              console.log(this.releaseDescriptionDatesArray,  "datesArr"); // data is not showing here 
            }
          }
        }
    console.log(this.releaseDescriptionDatesArray,  "datesArr"); // data doesn't appear here 
      }

Sample data

       datesArray =  [ {
           "type": "disasterRecovery",
           "date": "2019-07-28",
           "releaseWindow":       {
              "start": null,
              "end": null
           },
           "description": "Disaster Recovery",
           "linkable": true
        },
           {
           "type": "nooutageRelease",
           "date": "2019-08-03",
           "releaseWindow":       {
              "start": "2019-08-04T00:00:00",
              "end": "2019-08-04T23:59:59"
           },
           "description": "Infrastructure Release (No Outage)",
           "linkable": true
        }]

Answer №1

If you're aware that the attribute linkable exists in every item, there's no need to parse through each attribute and check if any of them are linkable, and then verify if it's true.

Moreover, since you are strictly comparing your values (===), 'true' !== true, which may be why your array is empty. To address this, you could utilize the filter function on your array with a callback that specifically checks if linkable is true. This approach will streamline your code significantly.

let datesArray = [{
        "type": "disasterRecovery",
        "date": "2019-07-28",
        "releaseWindow": {
            "start": null,
            "end": null
        },
        "description": "Disaster Recovery",
        "linkable": true
    },
    // Changed value of this item for testing purposes
    {
        "type": "nooutageRelease",
        "date": "2019-08-03",
        "releaseWindow": {
            "start": "2019-08-04T00:00:00",
            "end": "2019-08-04T23:59:59"
        },
        "description": "Infrastructure Release (No Outage)",
        "linkable": false
    }
];

// This callback ensures that date.linkable is not null,
// and its value is true.
// If the attribute were null (or undefined), it would evaluate as "false-y" and hence
// not be included in the final array.
// The last part also verifies that the value is an actual boolean (see Lain's comment)
let output = datesArray.filter((date) => date.linkable && date.linkable === true);

console.log(output)

Answer №2

item[attribute] === true

Ensure you are using boolean true, not the string 'true'

for (let item of datesArray) {
          for (let attribute of Object.keys(item)) {
            if (attribute === 'linkable' && item[attribute] === true) {
              releaseDescriptionDatesArray.push(item); // data found 
              console.log(attribute)
              console.log(this.releaseDescriptionDatesArray, "datesArr");
            }
          }
}

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

Using react-big-calendar exclusively for the month view

I need help customizing react-big-calendar to only show the month view and trigger a function when a date is selected. I want to remove all other functionalities related to week, day, agenda, and time display. Essentially, I just want to display the month- ...

Adjusting the navigation image as it passes through various div elements during scrolling

Is it possible to dynamically change an image in the navigation bar based on the user's scroll position? For example, I want pic1 to be displayed when the page content is at the top, then switch to pic2 once the user reaches the footer, and then back ...

The Ajax Control Upload consistently defaults to the default value and disregards any text input selected from the drop-down list

On my website, I have implemented an ajax control upload event that allows users to upload a zip file and then unzip it using zlib. The folder name for unzipping is created based on the selection from a dropdown list. However, I am facing some issues where ...

Angular Navbar-Toogle Not Functioning with Bootstrap 5

I encountered an error on my console related to Popper.js. Specifically, I am receiving the following error message: "scripts.js:7 Uncaught SyntaxError: Unexpected token 'export'." Due to this error, I suspect that my toggle button is not functio ...

Having difficulty with loading JSON data into jqGrid

Explaining my jqGrid definition: . . . datatype: 'json', //Setting the data type to JSON url:'<%=request.getContextPath()%>/servlet/AjaxManager?mode=9999&beginindex=0&totallimit=10&colname=policyname&sorttype=asc&apos ...

The jQuery function for $(window).scroll is not functioning as expected

My challenge is getting the scroll to reveal my scrollTop value I've been working with this code: $(document).ready(function(){ console.log('Hello!'); $(window).scroll(function(){ console.log('Scrolling...'); var wScroll = ...

Is there a way to retrieve a child's parents within an array.filter() callback function even if they were initially filtered out?

Today I stumbled upon the array.filter() method and its accompanying callback function. I have a collection of objects structured like this: var treeAry = [ {"id" : "200", "parent": "#", "type" : "1"}, {"id" : "300", "parent": "#", "type" : " ...

Solving Promises with Arrays in JavaScript

Currently, I am working on a project and facing an issue that I need help with. Let me give you some background on what I am trying to achieve. First, I am making concurrent API calls using Axios in the following manner: const [...result] = await Promise. ...

Utilizing jQuery to Perform Calculations with Objects

Can someone help me with a calculation issue? I need to calculate the number of adults based on a set price. The problem I'm facing is that when I change the selection in one of the dropdown menus, the calculation doesn't update and continues to ...

Is it advisable to load 10,000 rows into memory within my Angular application?

Currently, I am in the process of creating a customer management tool using Angular.js that will allow me to directly load 10,000 customers into the $scope. This enables me to efficiently search for specific data and manipulate it without the need for serv ...

The functionality of TypeScript's .entries() method is not available for the type 'DOMTokenList'

I'm currently working with a stack that includes Vue3, Vite, and TypeScript. I've encountered an issue related to DOMTokenList where I'm trying to utilize the .entries() method but TypeScript throws an error saying Property 'entries&apo ...

What is preventing me from updating the value of a variable in this manner?

Trying to understand the reason behind an issue with overwriting a value passed to an angularJS directive using an isolate scope (@). When attempting to change the value of vm.index with the following: vm.index = parseInt(vm.index, 10) It does not seem t ...

Step-by-step guide on adding a gallery image in Node.js

I need help with posting my gallery image to a nodeJs server. Here is the code I am currently using: vm.getImageSaveContactInst = function() { var options = { maximumImagesCount: 1, // Only selecting one image for this example width ...

Bar Chart Data Label Problem with HighCharts

My goal is to have the label positioned outside the bar stack. I attempted to achieve this using the code below, but it seems to be inconsistent in its results. dataLabels: { enabled: true, color: '#333', ...

Reduce the density of x-axis labels in highcharts

Do you have any input on Highcharts? This chart belongs to me: https://i.sstatic.net/OAjJJ.png I am looking to reduce the density of x-axis labels, similar to the y-axis. Your assistance is greatly appreciated. Edit: for instance, take a look at this ...

Turn off HTML5 Audio

There are 4 <audio> elements on a webpage. The client has asked for them to be available sequentially. Meaning, the first audio should play, then the rest should remain disabled until the first one finishes, and so on. In addition, they want the au ...

Set the minimum date for the jQuery datepicker

Having trouble with jQuery datepickers for selecting from and to dates? Currently, the code restricts the selection in the second datepicker to dates after the one selected in the first datepicker. However, what if you need to set the to datepicker to allo ...

Endless cycle plaguing Grunt tasks

Currently in the process of setting up a foundation Gruntfile.js for some upcoming projects. Recently started working on a new computer, so I had to rebuild everything from scratch. Used Homebrew to install Node and NPM, followed by installing Grunt global ...

"Return to the top" feature that seamlessly integrates with jQuery's pop-up functionality

Currently, I am facing an issue with a jQuery selectmenu list that opens as a popup due to its length. My goal is to add a "back to top" button at the end of the list. While researching online, I came across this tutorial which seems promising, but unfor ...

Issue with React-Native Picker - managing item selection

Encountering an issue with the Picker picker component. There is an array of currencies as strings. Using the Picker to select items from this array, and have a function included in the onValueChange prop in Picker. The problem arises when trying to select ...