Jam*_*mes 257 javascript arrays
该数组的每个项目都是一些数字.
var items = Array(523,3452,334,31, ...5346);
Run Code Online (Sandbox Code Playgroud)
如何使用新数组替换数组中的某些数字?
例如,我们想用1010替换3452,我们将如何做到这一点?
Eli*_*Eli 400
var index = items.indexOf(3452);
if (index !== -1) {
items[index] = 1010;
}
Run Code Online (Sandbox Code Playgroud)
另外,建议您不要使用构造函数方法初始化数组.相反,使用文字语法:
var items = [523, 3452, 334, 31, 5346];
Run Code Online (Sandbox Code Playgroud)
~如果您使用简洁的JavaScript并且想要缩短-1比较,您也可以使用运算符:
var index = items.indexOf(3452);
if (~index) {
items[index] = 1010;
}
Run Code Online (Sandbox Code Playgroud)
有时我甚至喜欢编写一个contains函数来抽象这个检查,并让它更容易理解正在发生的事情.什么是令人敬畏的是这适用于数组和字符串:
var contains = function (haystack, needle) {
return !!~haystack.indexOf(needle);
};
// can be used like so now:
if (contains(items, 3452)) {
// do something else...
}
Run Code Online (Sandbox Code Playgroud)
从字符串的ES6/ES2015开始,为阵列的ES2016开始,您可以更轻松地确定源是否包含其他值:
if (haystack.includes(needle)) {
// do your thing
}
Run Code Online (Sandbox Code Playgroud)
gil*_*ly3 82
该Array.indexOf()方法将取代第一个实例.要使用每个实例Array.map():
a = a.map(function(item) { return item == 3452 ? 1010 : item; });
Run Code Online (Sandbox Code Playgroud)
当然,这会创建一个新阵列.如果您想这样做,请使用Array.forEach():
a.forEach(function(item, i) { if (item == 3452) a[i] = 1010; });
Run Code Online (Sandbox Code Playgroud)
mua*_*f80 35
@gilly3 的回答很棒。
替换对象数组中的对象,保持元素的顺序不变
当我从服务器获取数据时,我更喜欢以下方式将新的更新记录更新到我的记录数组中。它使订单保持完整,并且非常直接地使用一个班轮。
users = users.map(u => u.id !== editedUser.id ? u : editedUser);
var users = [
{id: 1, firstname: 'John', lastname: 'Ken'},
{id: 2, firstname: 'Robin', lastname: 'Hood'},
{id: 3, firstname: 'William', lastname: 'Cook'}
];
var editedUser = {id: 2, firstname: 'Michael', lastname: 'Angelo'};
users = users.map(u => u.id !== editedUser.id ? u : editedUser);
console.log('users -> ', users);Run Code Online (Sandbox Code Playgroud)
小智 28
我建议的解决方案是:
items.splice(1, 1, 1010);
Run Code Online (Sandbox Code Playgroud)
拼接操作将从数组(即3452)中的位置1开始删除1项,并将其替换为新项1010。
Tes*_*rex 25
使用indexOf查找元素.
var i = items.indexOf(3452);
items[i] = 1010;
Run Code Online (Sandbox Code Playgroud)
Gom*_*oms 21
在一行中替换或更新数组项的最佳方法
array.splice(array.indexOf(valueToReplace), 1, newValue)
Run Code Online (Sandbox Code Playgroud)
例如:
let items = ['JS', 'PHP', 'RUBY'];
let replacedItem = items.splice(items.indexOf('RUBY'), 1, 'PYTHON')
console.log(replacedItem) //['RUBY']
console.log(items) //['JS', 'PHP', 'PYTHON']
Run Code Online (Sandbox Code Playgroud)
执行相同操作的另一种简单方法是:
items[items.indexOf(oldValue)] = newValue
Run Code Online (Sandbox Code Playgroud)
mel*_*okb 20
通过for循环轻松完成.
for (var i = 0; i < items.length; i++)
if (items[i] == 3452)
items[i] = 1010;
Run Code Online (Sandbox Code Playgroud)
Ame*_*icA 15
ES6方式:
const items = Array(523, 3452, 334, 31, ...5346);
Run Code Online (Sandbox Code Playgroud)
我们想替换3452为1010,解决方案:
const newItems = items.map(item => item === 3452 ? 1010 : item);
Run Code Online (Sandbox Code Playgroud)
当然,问题是多年前的问题,现在我更喜欢使用不可变的解决方案,当然,它对于ReactJS.
对于经常使用,我提供以下功能:
const itemReplacer = (array, oldItem, newItem) =>
array.map(item => item === oldItem ? newItem : item);
Run Code Online (Sandbox Code Playgroud)
Vir*_*oll 13
您可以使用索引编辑任意数量的列表
例如 :
items[0] = 5;
items[5] = 100;
Run Code Online (Sandbox Code Playgroud)
使用 ES6 扩展运算符和.slice方法替换列表中元素的不可变方式。
const arr = ['fir', 'next', 'third'], item = 'next'
const nextArr = [
...arr.slice(0, arr.indexOf(item)),
'second',
...arr.slice(arr.indexOf(item) + 1)
]
Run Code Online (Sandbox Code Playgroud)
验证是否有效
console.log(arr) // [ 'fir', 'next', 'third' ]
console.log(nextArr) // ['fir', 'second', 'third']
Run Code Online (Sandbox Code Playgroud)
在javascript中替换数组元素的功能方法:
const replace = (array, index, ...items) => [...array.slice(0, index), ...items, ...array.slice(index + 1)];Run Code Online (Sandbox Code Playgroud)
如果使用复杂的对象(甚至是简单的对象)并且可以使用es6,那Array.prototype.findIndex是一个很好的选择。对于OP的阵列,他们可以做到,
const index = items.findIndex(x => x === 3452)
items[index] = 1010
Run Code Online (Sandbox Code Playgroud)
对于更复杂的对象,这确实很有用。例如,
const index =
items.findIndex(
x => x.jerseyNumber === 9 && x.school === 'Ohio State'
)
items[index].lastName = 'Utah'
items[index].firstName = 'Johnny'
Run Code Online (Sandbox Code Playgroud)
小智 5
替换可以一行完成:
var items = Array(523, 3452, 334, 31, 5346);
items[items.map((e, i) => [i, e]).filter(e => e[1] == 3452)[0][0]] = 1010
console.log(items);Run Code Online (Sandbox Code Playgroud)
或创建一个函数以重用:
Array.prototype.replace = function(t, v) {
if (this.indexOf(t)!= -1)
this[this.map((e, i) => [i, e]).filter(e => e[1] == t)[0][0]] = v;
};
//Check
var items = Array(523, 3452, 334, 31, 5346);
items.replace(3452, 1010);
console.log(items);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
618006 次 |
| 最近记录: |