博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何从JavaScript中的数组替换元素?
阅读量:2533 次
发布时间:2019-05-11

本文共 2793 字,大约阅读时间需要 9 分钟。

In this article, we'll see various methods in action to replace an element from an array in JavaScript.

在本文中,我们将看到各种有效的方法来替换JavaScript中数组中的元素

Consider the following array,

考虑以下数组,

let heroes = [    "Captain America",    "Iron Man",    "Thor",    "Hulk",    "Loki"]console.log(heroes);

Output

输出量

(5) ["Captain America", "Iron Man", "Thor", "Hulk", "Loki"]

After the endgame, we are sure we will never see "Iron Man" again and our friendly neighborhood "spiderman" has very well joined the crew. So how do we replace Iron Man with SpiderMan in the heroes array?

比赛结束后,我们确信我们再也不会见到“钢铁侠”了,我们友好的邻居“蜘蛛侠”也加入了队伍。 那么,我们如何在英雄阵列中用蜘蛛侠代替钢铁侠?

The first approach is the naive approach where we can cycle through the array, find the element we had to replace and change it with the new element.

第一种方法是幼稚的方法,我们可以遍历数组,找到必须替换的元素,并使用新元素对其进行更改。

let heroes = [    "Captain America",    "Iron Man",    "Thor",    "Hulk",    "Loki"]console.log(heroes);for (let i = 0; i < heroes.length; i++) {
if (heroes[i] == "Iron Man") heroes[i] = "Spiderman";}console.log(heroes);

Output

输出量

(5) ["Captain America", "Iron Man", "Thor", "Hulk", "Loki"](5) ["Captain America", "Spiderman", "Thor", "Hulk", "Loki"]

Can we do a bit better?

我们可以做得更好吗?

We can use the spice() method on our array which is used to add and remove elements from an array. This method takes the first argument as an index which specifies the position of the element to be added or removed. The next argument it takes is the number of elements to be removed and is optional. The last argument is the new items added to the array.

我们可以在数组上使用spice()方法 ,该方法用于添加和删除数组中的元素。 此方法将第一个参数用作索引,该索引指定要添加或删除的元素的位置。 下一个参数是要删除的元素数,它是可选的。 最后一个参数是添加到数组的新项目。

array.splice(index,number,newItem);

Example:

例:

heroes.splice(1,1,"Spiderman");console.log(heroes);

Output

输出量

(5) ["Captain America", "Spiderman", "Thor", "Hulk", "Loki"]

Another way we can do this is by using the map method. The map method iterates through the array it is invoked on and runs a callback function on each value. It adds the result of that callback to the new array and returns an array at the end of the execution of callback of the same length.

我们可以执行此操作的另一种方法是使用map方法 。 map方法遍历在其上调用的数组,并对每个值运行一个回调函数。 它将回调的结果添加到新数组中,并在执行相同长度的回调结束时返回一个数组。

let newHeroes = heroes.map(hero => {
if (hero == "Iron Man") return "Spiderman"; else return hero;})console.log(newHeroes);

Output

输出量

(5) ["Captain America", "Spiderman", "Thor", "Hulk", "Loki"]

Since the map returns us a new array, we can use the new array obtained and copy that to our old array so that effectively our operation seems in place.

由于地图返回了一个新数组,因此我们可以使用获得的新数组并将其复制到旧数组中,以便有效地执行我们的操作。

heroes = newHeroes;console.log(heroes);

Output

输出量

(5) ["Captain America", "Spiderman", "Thor", "Hulk", "Loki"]

翻译自:

转载地址:http://xkvzd.baihongyu.com/

你可能感兴趣的文章
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------&gt;Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
接口测试-jmeter
查看>>
js便签笔记(5)——Dean Edwards大牛的跨浏览器AddEvent()设计(不知道是不是jQuery事件系统的原型)...
查看>>
重构wangEditor(web富文本编辑器),欢迎指正!
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
UIScrollerView ,UIPageControl混搭使用,添加定时器,无限循环滚动
查看>>
图论知识,博客
查看>>
微信企业号开发之-如何获取secret 序列号
查看>>
2015年最新Android基础入门教程目录(完结版)
查看>>