You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在for循环外部获取并使用循环内筛选出的Rice对象数组?

问题描述

我是JavaScript新手,若术语使用有误请见谅。我在for循环内创建数组,尝试筛选productname为Rice的数据,但在循环外打印时仅显示一行内容。希望能在for循环外部查看所有筛选出的Rice相关对象数组,代码如下:

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)
  }
}
问题原因与解决方法

问题根源

每次循环匹配到Rice数据时,你都用formatedata = [新对象]重新赋值了数组,而不是往数组里添加元素。这导致循环结束后,formatedata只保留了最后一次循环创建的单元素数组,所以外部打印只会显示一行。

修复后的代码

方法1:修正原循环逻辑

先在循环外初始化一个空数组,每次匹配到数据时用push()方法添加元素:

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 formatedata = [];
for(let i in datastructure){
  if(datastructure[i].productname === "Rice"){
    const { months, productname, production, hector } = datastructure[i];
    // 用push添加元素,而不是重新赋值
    formatedata.push({ months, productname, production, hector });
  }
}
// 循环外打印所有结果
console.log(formatedata);

方法2:用Array.filter()更简洁(推荐)

JS数组内置的filter()方法专门用来筛选元素,代码更简洁易读:

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"},
];
// 直接筛选出productname为Rice的所有对象
const formatedata = datastructure.filter(item => item.productname === "Rice");
console.log(formatedata);

额外提示

  • 尽量用let或const声明变量,避免使用var(var存在变量提升和作用域问题);
  • 遍历数组优先用for...of或者数组方法(如filter、map),for...in更适合遍历对象属性;
  • 可以用对象解构简化代码,比如const { months, productname } = datastructure[i],不用逐个赋值。

内容的提问来源于stack exchange,提问作者azowad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 02:54:24