简体   繁体   English

如何遍历 function 中的对象数组?

[英]How can I loop over array of objects in a function?

I have an array of objects, and I want to loop over certain values in the objects.我有一个对象数组,我想遍历对象中的某些值。 I tried to build a function for this, but am getting undefined back.我试图为此构建一个 function,但返回未定义。 What am I doing wrong here?我在这里做错了什么?

The Array:数组:

let clauseArray = [
    {
        clauseID: 0,
        clauseText: "text clause ",
    },

    {
        clauseID: 1,
        clauseText: "text clause ",
    },
    {
        clauseID: 2,
        clauseText: "text clause ",
    },
    {
        clauseID: 3,
        clauseText: "text clause ",
    },
];

And the function:和 function:

let displayClauses = function () {
    clauseArray.forEach((clause) => clause.clauseText);
};

Try this:试试这个:

let clauseArray = [
  {
    clauseID: 0,
clauseText:
"text clause ", 
  },

  {
    clauseID: 1,
    clauseText:
    "text clause ", 
  },
  {
    clauseID: 2,
    clauseText:
    "text clause ",    
  },
  {
    clauseID: 3,
    clauseText:
    "text clause ", 
  },
];

for (let value of clauseArray ) {
  
  console.log(value.text);
}

Thanks for the response everyone.谢谢大家的回复。 Really helpful.真的很有帮助。 forEach is just looping over the array but not returning anything. forEach 只是遍历数组但不返回任何内容。 :-) I know how to proceed from here. :-) 我知道如何从这里开始。

Learning to code is an uphill battle.学习编码是一场艰苦的战斗。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM