简体   繁体   English

Javascript Map function 链接

[英]Javascript Map function chaining

I just came across one thing and I cannot find an answer why is this happening so maybe someone can help.我刚刚遇到一件事,我找不到答案为什么会发生这种情况,所以也许有人可以提供帮助。

This is the piece of code这是一段代码

function titleCase(str) {
  let arr = str.split(" ")
    .map(s => s.charAt(0).toUpperCase() + s.substring(1).toLowerCase())
    console.log('arr1', arr)

let arr2 = str.split(" ")

  arr2.map(s => s.charAt(0).toUpperCase() + s.substring(1).toLowerCase())
  console.log('arr2', arr2)
}

titleCase("I'm a little tea pot");

I am interested in why arr1 (when map is chained immediately after split is working as expected. It returns ["I'm", "A", "Little", "Tea", "Pot"] , but arr2 is returning ["I'm", "a", "little", "tea", "pot"]我对为什么 arr1 感兴趣(当 map 在 split 按预期工作后立即链接时。它返回["I'm", "A", "Little", "Tea", "Pot"] ,但 arr2 正在返回["I'm", "a", "little", "tea", "pot"]

Should this type of writing (whether it is chained or not) return the same key?这种类型的写作(无论是否被链接)是否应该返回相同的键? What am I missing?我错过了什么?

In the former, your variable is being assigning the result of operations split followed by map on your array.在前者中,您的变量正在分配操作split的结果,然后是阵列上的map In the latter, your variable is being assigned only the result of split .在后者中,您的变量仅被分配split的结果。

So, this:所以这:

let arr = str.split(" ")
   .map(s => s.charAt(0).toUpperCase() + s.substring(1).toLowerCase())

is equivalent to:相当于:

let arr2 = str.split(" ")
arr2 = arr2.map(s => s.charAt(0).toUpperCase() + s.substring(1).toLowerCase())

ie you forgot to do the assignment arr2 = in case of map即你忘了做分配arr2 =map的情况下

Check out the documentation for map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map查看 map 的文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

The map() method creates a new array populated with the results map() 方法创建一个填充了结果的新数组

That means it doesn't change the original array, but gives you a new one.这意味着它不会更改原始数组,而是为您提供一个新数组。 So when you do所以当你这样做时

arr2.map(s => s.charAt(0).toUpperCase() + s.substring(1).toLowerCase())
console.log('arr2', arr2)

Map is returning a new array, but you have no variable to store the new array that.map creates. Map 正在返回一个新数组,但是您没有变量来存储.map 创建的新数组。 So所以

const result = arr2.map(s => s.charAt(0).toUpperCase() + s.substring(1).toLowerCase())
console.log('result', result)

Will work.将工作。

In contrast if we check out the documentation for sort: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort相反,如果我们查看排序文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

The sort() method sorts the elements of an array in place sort() 方法对数组的元素进行就地排序

So it is changing the same array and not giving you new one.所以它正在改变同一个数组而不是给你新的数组。

it is because in this line这是因为在这一行

let arr2 = str.split(" ")<----- this is what you get from console.log()

    arr2.map(s => s.charAt(0).toUpperCase() + s.substring(1).toLowerCase())
  console.log('arr2', arr2)
}

you are using console.log(arr2) which is assinged to be equal to str.split(" ")您正在使用console.log(arr2) ,它被分配为等于str.split(" ")

for you second line to work you have to assign arr2.map a variable and then you will be able to print it in the console like this为了让您第二行工作,您必须为 arr2.map 分配一个变量,然后您就可以像这样在控制台中打印它

const arr2=arr2.map(s => s.charAt(0).toUpperCase() + s.substring(1).toLowerCase())
      console.log('arr2', arr2)
    }

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

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