[Vuejs]-Need to iterate through arrays having objects Vue

1👍

This is simple JavaScript and has nothing to do with VueJS per se. The reason your iteration is not working is because you start with i = 1 while in coding you start with an index of 0. Also, you are including the last number with your comparison statement <= which is not in the array (because you start counting at 0, not at 1). On top of that, you can just print out object values by their keys. This all adds up to something like this:

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

0👍

Your top loop iterate should look like:

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

This code should work:

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
    }
  }
}

0👍

Hi the given list of array objects is unclear but if you are trying to iterate over JSON data type, you can use the code below. This code dynamically discovers the properties and return 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>

Leave a comment