1

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


}
1
  • Initialize the formatedata array outside the for loop and then do formatedata.push(thing you want to push in here); Commented Aug 11, 2022 at 10:09

2 Answers 2

1

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 }

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

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. Thanks for giving me time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.