简体   繁体   English

如何在不改变其他元素的情况下替换数组中的元素?

[英]How to replace an element in an array without changing the other elements?

If I have this array ['Jan', 'March', 'April', 'June'] .如果我有这个数组['Jan', 'March', 'April', 'June'] How to replace 'April' with 'newVal' and keep all other elements unchanged.如何用'newVal'替换'April'并保持所有其他元素不变。 I tried我试过

const months = ['Jan', 'March', 'April', 'June'];
months.splice(3, 1, 'newVal');
console.log(months);

I want it to return ['Jan', 'March', 'newVal', 'June'] but it returns ["Jan", "March", "April", "newVal"]我希望它返回['Jan', 'March', 'newVal', 'June']但它返回["Jan", "March", "April", "newVal"]

Why not assign directly?为什么不直接赋值?

 const months = ['Jan', 'March', 'April', 'June']; months[2] = 'newVal'; console.log(months);

You can do this:你可以这样做:

a.splice(2,1,"newVal")

It deletes 1 element at position 2 and adds the new value "newVal"它删除位置 2 处的 1 个元素并添加新值“newVal”

You could first find the Arrray.prototype.indexOf() some "string" , than you can directly replace it:你可以先找到Arrray.prototype.indexOf()一些"string" ,然后你可以直接替换它:

const arr = ["a", "b", "c", "d"];
const idx = arr.indexOf("c");       // 0..N or -1 if not found

// Make sure the index is found, than replace it
if (idx > -1) arr.splice(idx, 1, "newVal");

A nifty reusable function:一个漂亮的可重用函数:

 const replaceInArray = (arr, oldVal, newVal) => { const idx = arr.indexOf(oldVal); if (idx > -1) arr.splice(idx, 1, newVal); }; const months = ['Jan', 'March', 'April', 'June']; replaceInArray(months, "April", "newVal"); console.log(months)

如果您知道要替换的值(即:您总是想替换 'April'):

months[months.indexOf("April")] = 'newVal';

永远记住数组的第一个索引总是 0,所以只需这样做:

months.splice(2,1,"newVal")

The function which you used was correct but You mistook the index value of element april of the array months您使用的函数是正确的,但您误认为数组months的元素april的索引值

The correct index value of april is 2 4月的正确索引值为2

So your code will be like this所以你的代码会是这样的

 const months = ['Jan', 'March', 'April', 'June']; months.splice(2, 1, 'newVal'); console.log(months);

暂无
暂无

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

相关问题 更改一个数组元素会影响其他元素 - Changing one array element affects other elements 使用 jquery 替换 li 文本而不更改其他元素 - Replace li text using jquery without changing other elements React如何映射一个数组,其输入元素值独立于同一映射数组中的其他输入元素变化 - React How to map an array with input element value changing independently from other input elements in same mapped array 更改元素内的一行文本,而不更改其他嵌套元素 - Change a line of text inside element without changing other nested elements 如果我的javascript数组的值包含两个元素,如何使用一个元素而不使用另一个元素? - If my javascript array has values that have two elements how can I use one element without the other? javascript-如何将数组中的每个元素与其他数组中的其他元素合并? - javascript - How to merge each element in array with other elements in different array? 如何在不更改原始数组的情况下删除第一个数组元素? - How to remove first array element without changing original array? 如何用 Javascript 中的相同元素替换数组中的某些元素? - How to replace certain number of elements in array with a same element in Javascript? 如何在不影响画布中其他元素的情况下旋转特定元素? - How to rotate a particular element without affecting other elements in canvas? 在数组中插入元素取决于其索引位置,而不替换或更改angularjs中的其他元素 - Insert element in array depends on its index position without replace or change other element in angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM