Olubjay
Olubjay

Reputation: 1

Need to iterate through arrays having objects Vue

I have an array list and each list are groups of objects. I need to iterate through each group and check if an object in a list satisfies a condition.

This is what have been able to do but, doesn't iterate through each object.

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

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

This is the list of 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

Upvotes: 0

Views: 62

Answers (3)

Mahyar Mottaghi Zadeh
Mahyar Mottaghi Zadeh

Reputation: 1319

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>

Upvotes: 0

Elvin Bayramov
Elvin Bayramov

Reputation: 107

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

Upvotes: 0

Imre_G
Imre_G

Reputation: 2535

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

Upvotes: 1

Related Questions