?(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 ap
tag.
原文:https://stackoverflow.com/questions/35588559
最满意答案
在您的示例中,
map
和forEach
之间的本质区别在于forEach
对原始数组元素进行操作,而map
显式返回一个新的数组。使用
forEach
您将对原始数组中的每个元素进行一些操作(可选择更改)。forEach
方法运行您为每个元素提供的函数,但不返回任何内容(undefined
)。 另一方面,map
遍历数组,对每个元素应用一个函数,并将结果作为新的数组发出 。
forEach
的“副作用”是原来的数组正在改变。 使用map
“无副作用”意味着,在惯用语中,原始数组元素不改变; 新数组是原始数组中每个元素的一对一映射 - 映射变换是您提供的函数。没有数据库涉及的事实并不意味着您不必对数据结构进行操作,毕竟这是数据结构中任何语言的编程之一。 对于你最后一个问题,你的数组不仅可以包含数字,而且可以包含对象,字符串,函数等。
The essential difference between
map
andforEach
in your example is thatforEach
operates on the original array elements, whereasmap
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. TheforEach
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" withmap
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.
相关问答
更多-
javascript - 在Map.prototype.forEach()中获取索引(javascript - get index in Map.prototype.forEach())[2022-10-07]
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 ...
-
javascript forEach方法有什么用途(该地图无法做什么)?(what use does the javascript forEach method have (that map can't do)?)[2022-02-23]
在您的示例中, 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 ...
-
使用forEach方法更改项目的属性(javascript)(Change Properties of Items using a forEach Method (javascript))[2022-07-18]
删除引号如: var myArray = [div1,div2,div3] Remove the quotes like: var myArray = [div1,div2,div3] -
JavaScript - ForEach(JavaScript - ForEach)[2022-09-06]
您不需要使用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 ...
-
javascript forEach范围(javascript forEach scope)[2022-03-26]
您链接到州的文档 如果向forEach()提供thisArg参数,则在调用时它将被传递给回调,以用作其此值。 否则,将传递undefined的值以用作其此值。 最终通过回调可观察到的this值根据用于确定函数所见的通常规则来确定。 所以,要得到你想要的 tempGameState.forEach(function(...){ ... }, this)) ^^^^^^ The documentation you linked to states If a thisArg parameter is p ...