Looking to cycle through arrays containing objects using Vue

Within my array list, each group contains different objects. My goal is to iterate through each group and verify if any object in a list meets a specific condition.

Although I attempted to achieve this, my current code falls short of iterating through every individual object in the groups:

 for (i = 1; i <= this.portfolioDetails.length; i++) {

    for (var j = 1; j < this.portfolioDetails[i].length; j++) 
    {
       console.log(portfolioDetails[i][j]);
    }
 }

Here is a list of the array objects:

portfolioDetails:Array[3]
    0:Object
      ACCOUNTID:"S1001"
      ACCOUNTNAME:"Bla bla bla"
      ACCRUEDINTERESTALL:0
      PRICE:0.69
      UNITS:60.49
      VALUE:41.98
      product:null
    1:Object
      ACCOUNTID:"S1002"
      ACCOUNTNAME:"blo bla blo"
      ACCRUEDINTERESTALL:0
      PRICE:0.69
      UNITS:60.49
      VALUE:41.98
      product:null
    2:Object
      ACCOUNTID:"S1003"
      ACCOUNTNAME:"blik blik blik"
      ACCRUEDINTERESTALL:0
      PRICE:0.69
      UNITS:60.49
      VALUE:41.98
      product:null

Answer №1

The concept here involves basic JavaScript principles and is not directly related to VueJS. The issue with your iteration lies in the fact that you initialized i = 1 whereas, in programming, indexing typically starts at 0. Additionally, including the last number using the comparison operator <= is incorrect since arrays are zero-indexed. Instead, consider accessing object values by their respective keys for a more accurate approach:

for (let i = 0; i < this.portfolioDetails.length; i++) {
    console.log(this.portfolioDetails[i].ACCOUNTID)
}

Answer №2

It's important that your main loop is structured properly:

for (i = 0; i < this.portfolioDetails.length; i++) { ... }

Ensure your code follows this structure:

for (let i = 0; i < this.portfolioDetails.length; i--) {
  for (let j = 0; j < this.portfolioDetails[i].length; j--) 
  {
    // Check conditions here
    if (this.portfoiloDetails[i][j].ACCOUNTID === 'S1002') { 
     // Actions goes here
    }
  }
}

Answer №3

Hey there! If the list of array objects provided seems unclear, you can still iterate over JSON data types using the code snippet below. This piece of code is designed to dynamically explore the properties and retrieve the value of each property.

<script>
    var portfolioDetails = { 'data': [ 
         { 'fname': 'joe', 'lname': 'smith', 'number': '34'} , 
         { 'fname': 'jim', 'lname': 'Hoff', 'number': '12'} , 
         { 'fname': 'jack', 'lname': 'jones', 'number': '84'}   
    ] };

    //iterate over the records
    for (i = 0; i < portfolioDetails["data"].length; i++) {
       var data = this.portfolioDetails["data"][i];
       var propertiesCount = Object.getOwnPropertyNames(data).length;

       //iterate over the properties of each record
       for (var j = 0; j < propertiesCount; j++) 
       {
           var propName = Object.getOwnPropertyNames (data)[j];
           console.log(portfolioDetails["data"][i][propName]);

       }
       }
</script>

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

Problem encountered when utilizing the jQuery method .load()

Currently, there is an issue on my website where I am trying to load the content of a PHP file into a <div>. After ruling out any server-side problems, I have come to seek help with this question: Is there anything incorrect in the following code? & ...

Streamlining the process of implementing click events on elements selected by class using jQuery

Slowly but surely, I am gaining familiarity with jQuery and have reached the point where I desire to abstract my code. However, I am encountering issues when attempting to define click events during page load. Within the provided code snippet, my objectiv ...

The Vuetify v-img component fails to display images when using absolute paths

Having a bit of trouble here: I want to showcase an image using an absolute path in Vuetify with the v-img tag, but for some reason, it's not displaying. I attempted to use :src="require('path/to/img')", but it throws an error saying "This ...

utilizing vue model attributes with `${}`

I am encountering an issue with my code. Initially, :src="labels[index]" was working fine for me. However, as my codebase expanded, I needed to use ${app.labels[index]}. Simply using ${app.labels} works, but the addition of [index] causes it to b ...

Reactjs implemented with Material UI and redux form framework, featuring a password toggle functionality without relying on hooks

Currently, I am working on a react project where I have developed a form framework that wraps Material-UI around Redux Form. If you want to check out the sandbox for this project, you can find it here: https://codesandbox.io/s/romantic-pasteur-nmw92 For ...

CSS style takes precedence over inline JavaScript transitions occurring from a negative position

I am puzzled by a JavaScript fiddle I created which contains two small boxes inside a larger box. Upon clicking either of the buttons labeled "Run B" or "Run C", the corresponding box undergoes a CSS transition that is controlled by JavaScript. Below is t ...

Error: The 'path' value is undefined and cannot be searched for using the 'in' operator

Hey there, I'm currently trying to grasp the concept of vue-router. I've watched multiple tutorials but encountered an error that says 'Cannot use 'in' operator to search for 'path' in undefined,' and the router-link ...

Mapping an object in ReactJS: The ultimate guide

When I fetch user information from an API, the data (object) that I receive looks something like this: { "id":"1111", "name":"abcd", "xyz":[ { "a":"a", "b":"b", "c":"c" ...

Can you assist in resolving this logical problem?

exampleDEMO The code above the link demonstrates how to control input forms based on a button group selection. In this scenario, when a value is inputted on the 'First' Button's side, all input forms with the same name as the button group ...

Issues with the update of class properties in Node.js using Express

I am facing some challenges with a .js Object's attribute that is not updating as expected. Being new to the world of JavaScript, I hope my problem won't be too difficult to solve. To begin with, here is a snippet from my Node class: Node = fu ...

Utilize an image in place of text (script type="text/javascript")

The vendor has provided me with some code: <a class="sh_lead_button" href="https://107617.17hats.com/p#/lcf/sfrnrskrvhcncwvnrtwwvhxvzkrvzhsd" onclick="shLeadFormPopup.openForm(event)">FREE Puppies</a> <script type="text/javascript" src="htt ...

How to retrieve a variable from an object within an array using AngularJS code

I recently started learning TypeScript and AngularJS, and I've created a new class like the following: [*.ts] export class Test{ test: string; constructor(foo: string){ this.test = foo; } } Now, I want to create multiple in ...

Change the size of the individual cells within JointJS

I have some code for a jointjs demo that includes basic shapes on a paper. I am looking to adjust the size of the shapes or highlight them when clicked on or when the cursor moves over them. var graph = new joint.dia.Graph; v ...

The select2 plugin seems to be having trouble recognizing the Li click functionality

I have a select menu that is using the select2 plugin. I am trying to add a class to the <select> element when a menu option is clicked, but it doesn't seem to be working as expected even after testing with an alert. https://jsfiddle.net/ysm4qh ...

What is the best method for establishing environment variables across different platforms?

When working with Node scripts on Windows, the format should be like this: "scripts": { "start-docs": "SET NODE_ENV=development&&babel-node ./docs/Server.js" } However, on Linux, the SET command is not used, so it would look like this: "scri ...

Having trouble getting this.$set() to function properly with vue2.0 and Laravel integration

I'm currently attempting to store data in vue js and then display it on the view. This is my vue method: getVueItems: function(){ var vm = this; axios.get('/someuri').then(function(response) { vm.$set(this,'it ...

Tracking tool for monitoring progress of HTTP POST requests

Hi, I am still a beginner when it comes to NodeJS and its modules. I could really use some assistance with creating a progress bar for an application I'm working on. Right now, the progress bar only reaches 100% upon completion and I suspect my piping ...

Retrieve the Data from Input Fields with Matching Classes and Transmit to a Script Using AJAX Request

I am working on a form that includes multiple input fields: <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="da ...

Implement Vue router (version 4) in a vanilla .js file within the Quasar framework (version 2) for seamless navigation and

I'm attempting to access the router from a plain .js file within a Quasar project but I am encountering difficulties. In my search on how to achieve this in Vue, I found that people typically import the Router object from /src/router/index, like so: i ...

The Angular material datepicker is not accurately capturing the date I am trying to select

I am facing an issue with the Angular Material datepicker where it does not select the date I choose. Instead, in my AngularJS controller, I always get the sysdate even if I select another date. Can anyone help me figure out what I am doing wrong? Here is ...