我知道有很多这样的话题.我知道基础知识:.forEach()在原始数组和.map()新数组上运行.
就我而言:
function practice (i){
return i+1;
};
var a = [ -1, 0, 1, 2, 3, 4, 5 ];
var b = [ 0 ];
var c = [ 0 ];
console.log(a);
b = a.forEach(practice);
console.log("=====");
console.log(a);
console.log(b);
c = a.map(practice);
console.log("=====");
console.log(a);
console.log(c);
Run Code Online (Sandbox Code Playgroud)
这是输出:
[ -1, 0, 1, 2, 3, 4, 5 ]
=====
[ -1, 0, 1, 2, 3, 4, 5 ]
undefined
=====
[ -1, 0, 1, 2, 3, 4, 5 ] …Run Code Online (Sandbox Code Playgroud) 我在map和foreach中看到的唯一区别map是返回一个数组而forEach不是.但是,我甚至不理解方法的最后一行forEach" func.call(scope, this[i], i, this);".例如,是不是" this"和" scope"指的是同一个对象而不是this[i]并且i指的是循环中的当前值?
我在另一篇文章中注意到有人说" forEach当你想根据列表的每个元素做某事时使用.例如,你可能会在页面上添加内容.基本上,它非常适合你想要的"副作用".我不知道副作用是什么意思.
Array.prototype.map = function(fnc) {
var a = new Array(this.length);
for (var i = 0; i < this.length; i++) {
a[i] = fnc(this[i]);
}
return a;
}
Array.prototype.forEach = function(func, scope) {
scope = scope || this;
for (var i = 0, l = this.length; i < l; i++) {
func.call(scope, this[i], i, this);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,这些方法在javascript中是否有任何实际用途(因为我们不更新数据库),除了操纵这样的数字:
alert([1,2,3,4].map(function(x){ …Run Code Online (Sandbox Code Playgroud) 当我开始编写Scala时,我确实有一个很大的疑问.我想知道mapscala中的方法是如何工作的.是顺序处理还是多线程处理?更重要的是,我想知道为什么map方法比while或更快foreach?
val list = List(1,2,3,45,12)
list.map(x => x)
list.foreach(x => println(x))
Run Code Online (Sandbox Code Playgroud) 我有一个 DataFrame,其中一列有逗号分隔的数据。
例如:数据看起来像这样:[{value:1}, {value:2, value:3}, {some value}, {somevalue, othervalue}]
该列是字符串数据类型。我想将它转换为 List 并应用一些函数。现在我有一个函数可以将字符串列转换为列表和其他应用逻辑。
但是哪个函数会更好和优化,因为我们有 2 个类似的声音函数mapPartitions和foreachPartitions,它是否具有完全相同的性能以及在什么场景下使用哪个?
我目前正在学习 JavaScript。我尝试使用 foreach 循环来更新数组中的元素。但问题是“console.log”结果总是和以前一样的数组。下面是代码。谁能帮忙说说问题?
var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139
];
var addNum = function(element,index,array){
if(element%3===0)
{
element += 100;
}
};
test.forEach(addNum);
console.log(test);
Run Code Online (Sandbox Code Playgroud) 这里有一点我不明白。map几天前我刚刚了解到的这个函数应该是一个了不起的函数,如果我找到它的用途,它将改变我编写代码的方式。但我仍然看不出它与forEach. 唯一的区别是传递给的函数map用return值替换了当前元素。但是forEach也可以这样做,这意味着它map只是forEach.
MDN 上的示例:
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3], numbers is still [1, 4, 9]
Run Code Online (Sandbox Code Playgroud)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
好吧,很酷,我猜?
我可以这样做forEach:
var numbers = [1, 4, 9];
var roots = numbers.forEach(function(el){el=Math.sqrt(el);});
// roots is now [1, 2, 3], numbers is still [1, 4, 9]
Run Code Online (Sandbox Code Playgroud)
其他例子:
var kvArray = [{key:1, value:10}, {key:2, value:20}, {key:3, value: 30}]; …Run Code Online (Sandbox Code Playgroud) 我尝试了两种方法来制作一个列表
var response = List[RS_TxnNested]
consumertxnlist.foreach(txData => {
response = RS_TxnNested(blabla) +: response
})
Run Code Online (Sandbox Code Playgroud)
其中consumertxnlist是Seq [something].
另一种方式是
var response = consumerTxnList._2.map(txData => RS_TxnNested(blabla))
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我澄清哪一个更好,为什么?
javascript ×4
arrays ×2
foreach ×2
scala ×2
apache-spark ×1
dictionary ×1
loops ×1
prototype ×1
pyspark ×1
pyspark-sql ×1