Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
How To Use An Array Of Object Outside For Loop Created In The For Loop?

I am a newbie in Javascript. So, forgive me for any mistakes related to terminology. I have created an array inside a for loop. Here is the code. Whenever I use console outside the condition and loop only a single line is shown. I have updated the question and added the array. I want to select the array which has rice and see it outside of the for loop.

enter image description here

I need to see like this whenever I use console outside the loop: enter image description here

  const datastructure= [

    {"months":"January","productname":"Rice","production":"10","hector":"2"},
    {"months":"February","productname":"Rice","production":"11","hector":"2"},
    {"months":"March","productname":"Rice","production":"12","hector":"2"},
    {"months":"April","productname":"Rice","production":"13","hector":"2"},
    {"months":"January","productname":"Fruit","production":"14","hector":"2"},
    {"months":"February","productname":"Fruit","production":"15","hector":"2"},
    {"months":"Markch","productname":"Fruit","production":"16","hector":"2"},
    {"months":"April","productname":"Fruit","production":"16","hector":"2"},

];
for(i in datastructure){
  if(datastructure[i].productname=="Rice")
  {
  var months=datastructure[i].months
  var  productname=datastructure[i].productname
  var  production=datastructure[i].production
  var  hector=datastructure[i].hector



formatedata =[({'months':months, 'productname':productname,'production':production,'hector':hector})];


 console.log(formatedata)
  }


}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think you need to use .filter instead of a for loop

const datastructure= [ 
  {"months":"January","productname":"Rice","production":"10","hector":"2"},
  {"months":"February","productname":"Rice","production":"11","hector":"2"},
  {"months":"March","productname":"Rice","production":"12","hector":"2"},
  {"months":"April","productname":"Rice","production":"13","hector":"2"},
  {"months":"January","productname":"Fruit","production":"14","hector":"2"},
  {"months":"February","productname":"Fruit","production":"15","hector":"2"},
  {"months":"Markch","productname":"Fruit","production":"16","hector":"2"},
  {"months":"April","productname":"Fruit","production":"16","hector":"2"}
];

const riceProducts = datastructure.filter(({ productname }) => productname === 'Rice');

riceProducts.forEach(( product )=> console.log(product));

// or just 
// console.log(riceProducts)
.as-console-wrapper { max-height: 100% !important; top: 0 }

about 4 years ago · Juan Pablo Isaza Report

0

You will have to push to the formatedata array instead of reassigning it

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!