不能替换数组中的项
Can't replace an item inside of an array
编写Angular应用程序。
在此代码中,我试图替换数组中特定数字的项。
for (i=0;i<this.marked.length;i++) {
this.index = this.marked[i].ind;
this.newSongs[this.index] = this.marked[i].name;
}
这就是这。标记看起来像:
{
ind: index,
name: songName
}
那么为什么结果是这样的呢?
['name1', 2: 'name3']
代替:
['name1', 'name2', 'name3']
对不起,伙计们,它实际上是this.marked[i].ind;我不小心删掉了。问题没有解决
Khalid和Joel给了你正确的建议。您需要使用:
this.index = this.marked[i].ind
这里有一个angularJs提琴给你。希望这对你有帮助
https://jsfiddle.net/trollr/3adngkmx/function SongCtrl($scope) {
$scope.songs = [
{
ind: 1,
name: "Tessie"
},
{
ind: 2,
name: "Kiss Me I’m Shitfaced"
},
{
ind: 3,
name: "The State of Massachusets"
}
];
$scope.replace = function(index, value) {
for (i=0;i<$scope.songs.length;i++) {
var currentIndex = $scope.songs[i].ind;
if(currentIndex == index) {
$scope.songs[i].name = value;
}
}
}
}
我想你只需要把
this.index = this.marked[i]
this.index = this.marked[i].ind
我注意到我试图在循环中运行我的代码。所以当我把它拔出来的时候它正常工作了。
相关文章:
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- Javascript替换字节数组序列
- 如何检查和替换数组中的值
- Javascript:字符串中有效的基于数组的替换
- 用数组(javascript)中的值替换regex捕获
- 将数组存储到本地存储中,而不是替换
- 将对象值替换为外部数组中的值
- 如何在 JavaScript 中将数组中的 [ ] 替换为 var 结果
- 合并/替换Javascript数组
- 使用Javascript regex替换“index”数组字符串
- 将数组值替换为循环以创建新数组
- 无法替换Javascript中对象数组的键
- JavaScript:替换数组中的字符串
- 用字符替换将字符串之类的数组替换为数组
- 挖空 JS - 数组替换,同时保留相同的元素
- API/应用程序设计:用新数组替换非常大的数组或修改现有数组
- jQuery:用数组替换数组
- 用数组替换重复的代码,并对其进行迭代
- 如何用jQuery对象数组替换一个DOM元素?
- DOM -用节点数组替换节点(有效地)