首页 \ 问答 \ 有没有更好的方法在文本块中添加换行符而不是在每个项目之后添加
?(Is there a better way to add line-breaks in a block of text than just putting
after every item?)

有没有更好的方法在文本块中添加换行符而不是在每个项目之后添加
?(Is there a better way to add line-breaks in a block of text than just putting
after every item?)

我有一个很大的项目列表,其格式如下:

list item <br>
list item <br>
list item <br>
list item <br>
list item <br>
list item <br>

等等

目前,我已经通过在每个项目之后添加br来让他们工作,但它有点混乱。 是否有一些我可以使用的CSS或HTML会在每个项目后自动断行? 它们目前都在p标签内。


I have a big list of items that are formatted like so:

list item <br>
list item <br>
list item <br>
list item <br>
list item <br>
list item <br>

etc.

Currently, I've got them to work by putting a br after every item, but it's kinda messy. Is there some CSS or HTML that I can use that will automatically break the line after each item? They are all currently inside a p tag.


原文:https://stackoverflow.com/questions/35588559
更新时间:2024-04-24 14:04

最满意答案

在您的示例中, mapforEach之间的本质区别在于forEach对原始数组元素进行操作,而map显式返回一个新的数组。

使用forEach您将对原始数组中的每个元素进行一些操作(可选择更改)。 forEach方法运行您为每个元素提供的函数,但不返回任何内容( undefined )。 另一方面, map遍历数组,对每个元素应用一个函数,并将结果作为新的数组发出

forEach的“副作用”是原来的数组正在改变。 使用map “无副作用”意味着,在惯用语中,原始数组元素改变; 新数组是原始数组中每个元素的一对一映射 - 映射变换是您提供的函数。

没有数据库涉及的事实并不意味着您不必对数据结构进行操作,毕竟这是数据结构中任何语言的编程之一。 对于你最后一个问题,你的数组不仅可以包含数字,而且可以包含对象,字符串,函数等。


The essential difference between map and forEach in your example is that forEach operates on the original array elements, whereas map explicitly returns a new array as a result.

With forEach you are taking some action with -- and optionally changing -- each element in the original array. The forEach method runs the function you provide for each element, but returns nothing (undefined). On the other hand, map walks through the array, applies a function to each element, and emits the result as a new array.

The "side effect" with forEach is that the original array is being changed. "No side effect" with map means that, in idiomatic usage, the original array elements are not changed; the new array is a one-to-one mapping of each element in the original array -- the mapping transform being your provided function.

The fact that there's no database involved does not mean that you won't have to operate on data structures, which, after all, is one of the essences of programming in any language. As for your last question, your array can contain not only numbers, but objects, strings, functions, etc.

相关问答

更多
  • Map.prototype.forEach用两个参数进行回调:值和键。 不,它用三个参数调用 ,就像Array#forEach 。 第三个是地图。 是否有可能获得每个条目的索引,类似于Array.prototype.forEach(functcion(value, index) => {}) (相当肯定, functcion部分并不意味着在那里。) 这是key所在。 没有单独的“索引”。 Map的迭代次序是为各种迭代操作定义的,但是没有直接的迭代构造给你索引 (而不是键 )的顺序。 订单是原始的关键广告订单 ...
  • []是一个数组。 这个数组根本不用。 它被放在页面上,因为使用数组可以访问阵列原型,如.forEach 。 这比键入Array.prototype.forEach.call(...);要快Array.prototype.forEach.call(...); 接下来, forEach是一个将函数作为输入的函数... [1,2,3].forEach(function (num) { console.log(num); }); ...对于this ( this是数组式的)中的每个元素,因为它有一个length ...
  • 在您的示例中, map和forEach之间的本质区别在于forEach对原始数组元素进行操作,而map显式返回一个新的数组。 使用forEach您将对原始数组中的每个元素进行一些操作(可选择更改)。 forEach方法运行您为每个元素提供的函数,但不返回任何内容( undefined )。 另一方面, map遍历数组,对每个元素应用一个函数,并将结果作为新的数组发出 。 forEach的“副作用”是原来的数组正在改变。 使用map “无副作用”意味着,在惯用语中,原始数组元素不改变; 新数组是原始数组中每个 ...
  • 你不能从一个forEach打破。 我可以想到三种方式来伪造它。 丑陋的方式 :将第二个参数传递给forEach 用作上下文 ,并在其中存储一个布尔值,然后使用if 。 这看起来很可怕 2.有争议的方式 :在try-catch块中围绕整个事物,当你想要破解时抛出异常。 这看起来很糟糕, 可能会影响性能 ,但可以封装。 有趣的方式 :使用every() 。 ['a', 'b', 'c'].every(function(element, index) { // Do your thing, then: i ...
  • 如果找到,您可以使用Array#some的短路并返回存储的节点。 some()为数组中存在的每个元素执行一次callback函数,直到找到callback函数返回一个真值(在转换为布尔值时变为true值)为止。 function getNodeById(currentNode, id) { var node; if (currentNode.id === id) { return currentNode; } currentNode.children.some ...
  • 删除引号如: var myArray = [div1,div2,div3] Remove the quotes like: var myArray = [div1,div2,div3]
  • 您不需要使用this , self或任何其他上下文来调用helpPerson。 直接调用它: var people = [ { id:1, firstName: 'Joe', lastName: 'Smith' }, { id:2, firstName: 'Bill', lastName: 'Smith' } ]; function doSomething() { people.forEach(function(person) { helpPerson(person); }); } ...
  • 这是使用filter和forEach方法的解决方案,使用callback函数。 见这里的参考: 过滤方法 var students = [ { firstname: "stud1", lastname: "stud2", marks: "60" }, { firstname: "stud3", lastname: "stud4", marks: "30" }, { firstname: "stud5", lastname: "stud6", marks: "70" }, ...
  • 您没有从map函数返回任何内容,因此隐式返回值undefined ,因此您的数组包含13个undefined值。 suits.forEach需要return suits.map 。 这将给出一个包含13个元素的数组,其中每个元素是一个包含四个元素的数组,其中内部数组的每个元素都是一个两个元素[suit, value]数组。 然后,您可以将顶级数组reduce为您所追求的52个元素数组: var newDeck = values.map(function(xValue) { return suits.ma ...
  • 您链接到州的文档 如果向forEach()提供thisArg参数,则在调用时它将被传递给回调,以用作其此值。 否则,将传递undefined的值以用作其此值。 最终通过回调可观察到的this值根据用于确定函数所见的通常规则来确定。 所以,要得到你想要的 tempGameState.forEach(function(...){ ... }, this)) ^^^^^^ The documentation you linked to states If a thisArg parameter is p ...

相关文章

更多

最新问答

更多
  • CSS修复容器和溢出元素(CSS Fix container and overflow elements)
  • SQL多个连接在与where子句相同的表上(SQL Multiple Joins on same table with where clause)
  • nginx 80端口反向代理多个域名,怎样隐藏端口的
  • xcode提醒样式,swift 3(xcode alert style, swift 3)
  • 在Chrome控制台中调试JavaScript(debugging javascript in Chrome console)
  • Javascript - 试图围绕自定义事件(Javascript - Trying to wrap my head around custom events)
  • 边栏链接不可点击(Sidebar links aren't clickable)
  • 使用recpatcha gem时如何显示其他表单错误?(How do I display other form errors when using the recpatcha gem?)
  • boost.python避免两次注册内部类,但仍然在python中公开(boost.python Avoid registering inner class twice but still expose in python)
  • Android 现在软件很少吗?以后会多起来吗
  • 如何在ActiveAdmin 0.5.0中为资源全局指定预先加载?(How to specify eager loading globally for a resource in ActiveAdmin 0.5.0?)
  • matlab代码为黄金比例持续分数(matlab code for golden ratio continued fraction)
  • Android浏览器触摸事件位置(Android browser touch event location)
  • 将cURL输出分配给Bash中的变量(Assign output to variable in Bash)
  • 我如何在MVC视图上没有时间获取当前日期(how i can get current date without time on MVC view)
  • sql连接函数(sql join of function)
  • 为什么在Xamarin Media插件中使用ImageSource.FromStream而不是FromFile?(Why use ImageSource.FromStream instead of FromFile in Xamarin Media plugin?)
  • 这段代码是否真的可以防止SQL注入?(Will this code actually work against SQL-injection? [duplicate])
  • 信阳方远计算机学校大专证被国家认可么
  • React / Rails AJAX POST请求返回404(React/Rails AJAX POST request returns 404)
  • Android与php服务器交互(Android interact with php server)
  • 自动刷新QTableWidget可能吗?(Refresh QTableWidget automatically possible?)
  • JVM / Compiler优化对象的未使用属性(optimization of unused properties of object by JVM / Compiler)
  • 插入表格时,乌克兰字符会更改为问号(Ukrainian character change to question mark when insert to table)
  • 在头文件中包含异常类(Including an exception class in a header file)
  • 完成c#中的执行后关闭sqlcmd(Close sqlcmd after finishing executing in c#)
  • 使用软导航栏正确检测屏幕尺寸(Detecting screensize correctly with soft navigation bar)
  • Typescript:从输入更新值(Typescript : update value from input)
  • 如何在执行某些行后仅在断点处停止?(How to only stop at a breakpoint after some line was executed?)
  • 以未定义的元素在JSON中循环(loop in JSON with undefined elements)