admin管理员组文章数量:1279176
I'm using AngularJS to create a site.
There's my array:
$scope.replyList = [
{
"id": "85485",
"reply_content": "aaaaaaa",
"reply_gender": "1",
"reply_author": "John"
},
{
"id": "85487",
"reply_content": "bbbbbbb",
"reply_gender": "1",
"reply_author": "Ben"
},
{
"id": "85504",
"reply_content": "ccccccc",
"reply_gender": "1",
"reply_author": "Wang"
}
]
What I want to do is update the item value by a given key id
.
For example, I'd like to update the content with id 85485
. How can you deal with that?
$scope.replyList[{id: 85475}].reply_content = 'dddddd'; /* failed */
Thank you very much.
I'm using AngularJS to create a site.
There's my array:
$scope.replyList = [
{
"id": "85485",
"reply_content": "aaaaaaa",
"reply_gender": "1",
"reply_author": "John"
},
{
"id": "85487",
"reply_content": "bbbbbbb",
"reply_gender": "1",
"reply_author": "Ben"
},
{
"id": "85504",
"reply_content": "ccccccc",
"reply_gender": "1",
"reply_author": "Wang"
}
]
What I want to do is update the item value by a given key id
.
For example, I'd like to update the content with id 85485
. How can you deal with that?
$scope.replyList[{id: 85475}].reply_content = 'dddddd'; /* failed */
Thank you very much.
Share Improve this question asked Sep 20, 2016 at 17:22 BenyiBenyi 9522 gold badges11 silver badges27 bronze badges3 Answers
Reset to default 6Use Array#find
method to get the array element.
$scope.replyList.find(function(v) {
return v.id == 85475;
}).reply_content = 'dddddd';
// or with ES6 arrow function
$scope.replyList.find(v => v.id == 85475).reply_content = 'dddddd';
var replyList = [{
"id": "85475",
"reply_content": "aaaaaaa",
"reply_gender": "1",
"reply_author": "John"
}, {
"id": "85487",
"reply_content": "bbbbbbb",
"reply_gender": "1",
"reply_author": "Ben"
}, {
"id": "85504",
"reply_content": "ccccccc",
"reply_gender": "1",
"reply_author": "Wang"
}];
replyList.find(function(v) {
return v.id == 85475;
}).reply_content = 'dddddd';
console.log(replyList);
For older browser check polyfill option of find method.
UPDATE : If you want to update all the element with that particular id
then use Array#forEach
method to iterate. Actually, there is no need of Array#find
method since you just want to update the value.
$scope.replyList.forEach(function(v) {
if(v.id == 85475) v.reply_content = 'dddddd';
});
var replyList = [{
"id": "85475",
"reply_content": "aaaaaaa",
"reply_gender": "1",
"reply_author": "John"
}, {
"id": "85487",
"reply_content": "bbbbbbb",
"reply_gender": "1",
"reply_author": "Ben"
}, {
"id": "85504",
"reply_content": "ccccccc",
"reply_gender": "1",
"reply_author": "Wang"
}];
replyList.forEach(function(v) {
if (v.id == 85475) v.reply_content = 'dddddd';
});
console.log(replyList);
Use the .filter
method of array and get the first position of the result
$scope.replyList.filter(function(item){
return item.id === '85475';
})[0].reply_content = 'dddddd';
If exists more than one result with the given value of key, process them all using the .map
method.
$scope.replyList
.filter(function(item){
return item.id === '85475';
})
.map(function(item){
item.reply_content = 'ddddd'
return item;
});
Take advantage of the javascript functional approach!!
Use a simple for loop to iterate over the array and find the object with the matching id and replace the content.This will work in all browsers.
function replace(array, id, content) {
var len = array.length,
i;
for (i = 0; i < len; i++) {
if (array[i].id === id) {
array[i].reply_content = content;
}
}
};
replace($scope.replyList, '85485', 'replacedcontent');
本文标签: javascriptAngularJS Update Array Items by a Given KeyStack Overflow
版权声明:本文标题:javascript - AngularJS Update Array Items by a Given Key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741254413a2366394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论