node.js 如何在 Lodash 中遍历数组中的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33010096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:21:51  来源:igfitidea点击:

How to iterate through objects within array in Lodash

node.jsunderscore.jslodash

提问by Ken

I'm trying to use lodash within an HTML template for an email in Node.js. I have one array with several objects. I would like to iterate through each object and list all of the repeating values. When I use the code below, I receive an error stating that the value is undefined (e.g., ReferenceError: firstName is not defined). The HTML template is in a separate file.

我正在尝试在 HTML 模板中使用 lodash 来处理 Node.js 中的电子邮件。我有一个包含多个对象的数组。我想遍历每个对象并列出所有重复值。当我使用下面的代码时,我收到一个错误,指出该值未定义(例如,ReferenceError: firstName is not defined)。HTML 模板位于单独的文件中。

Any thoughts on what I'm doing wrong?

对我做错了什么有任何想法吗?

Javascript:

Javascript:

var template = fs.readFileSync('server/views/email-template.html').toString();
var htmlAll = _.template(template)(orderInfo);

HTML:

HTML:

<% _.forEach(function(firstName) { %><%- firstName %></td><% }); %> <% _.forEach(function(lastName) { %><%- lastName %></td><% }); %>
<% _.forEach(function(address) { %><%- address %></td><% });%>
<% _.forEach(function(city) { %><%- city %><% }); %>, <% _.forEach(function(state.code) { %><%- state.code %><% });
%> <% _.forEach(function(zip) { %><%- zip %><% }); %>

<% _.forEach(function(item) { %><td><%- item %></td><% }); %>
<% _.forEach(function(cost) { %><td><%- cost %></td><% }); %>

Array:

大批:

[
  {
    "firstName": "John",
    "lastName": "Doe",
    "address": "123 Broadway",
    "city": "New York",
    "state": {
      "code": "NY",
      "state": "New York"
    },
    "zip": "10001",
  },

  {
    "color": "White",
    "size": "M",   
    "item": "T-Shirt",
    "cost": 19.99,
  },
  {
    "color": "Blue",
    "size": "L",   
    "item": "T-Shirt",
    "cost": 19.99,
  }
]

回答by Haseeb Asif

There are couple of things wrong here actually. Starting with template syntax, you are only specifying the iterator to foreach in your template but we need data and iterator both something as follows

实际上这里有几件事是错误的。从模板语法开始,您只在模板中指定要 foreach 的迭代器,但我们需要数据和迭代器,如下所示

_.forEach(users, function(user){

Also you have multiple objects in single array position so I have update the json structure a little bit as well.

此外,您在单个数组位置有多个对象,因此我也稍微更新了 json 结构。

After minor updates template something like this with some property missing intentionally,

在小更新模板之后,故意缺少一些属性,

var tmpl = _.template('<ul><% _.forEach(users, function(user) { %><li><%- user.firstName %></li><li><%- user.address %></li><li><%- user.state.code %></li><li><%- user.state.state %></li><ol> <% _.forEach(user.orders, function(order) { %> <li><span><%- order.item %><span> cost is: <span><%- order.cost %><span></li> <% }); %> </ol> <% }); %></ul>');

and json with orders arrays in same object is as follows

和在同一个对象中有订单数组的 json 如下

var data = { 'users': 

        [
  {
    "firstName": "John",
    "lastName": "Doe",
    "address": "123 Broadway",
    "city": "New York",
    "state": {
      "code": "NY",
      "state": "New York"
    },
    "zip": "10001",
    "orders": [
        {
            "color": "White",
            "size": "M",   
            "item": "T-Shirt",
            "cost": 19.99,
        },
        {
            "color": "Blue",
            "size": "L",   
            "item": "Pant",
            "cost": 19.99,
        }
  ]  
  } 
]
};

You can see it working on here at JSBIN

您可以在JSBIN上看到它正在运行

回答by John Meyer

You can also do this in ES6 arrow function style:

您也可以在 ES6 箭头函数样式中执行此操作:

const users = ['Hyman', 'Jill']; _.forEach(users, user => {console.log(user);});

const users = ['Hyman', 'Jill']; _.forEach(users, user => {console.log(user);});

回答by Magus

It's because you give orderInfo[0]to your template. And in your array, orderInfo[0]is just this part :

这是因为你给orderInfo[0]了你的模板。在你的数组中,orderInfo[0]只是这部分:

{
    "firstName": "John",
    "lastName": "Doe",
    "address": "123 Broadway",
    "city": "New York",
    "state": {
      "code": "NY",
      "state": "New York"
    },
    "zip": "10001",
}

So the template can't iterate on the missing values.

所以模板不能迭代缺失值。