我正在尝试更新数组中的一个值。基本上我想更新数组中一个对象的状态值。这是我的数组
[
{
"projects": {
"projectname": "one",
"dateadded": "16 April 2018, 7:23AM",
"status": 1
}
},
{
"projects": {
"projectname": "two",
"dateadded": "16 April 2018, 7:23AM",
"status": 1
}
},
{
"projects": {
"projectname": "three",
"dateadded": "16 April 2018, 7:32AM",
"status": 1
}
}
]
这是我的尝试,但它会更新数组中的所有内容。 参数1是项目的名称 - 以查看我是否在正确的项目上
archive(){
var parameter1 = this.navParams.get('param1');
this.storage.get('projectsStore').then(data => {
for (var i = 0; i < data.length; i++) {
if (data[i].projects.projectname == parameter1) {
this.project = [{ "projectname":data[i].projects.projectname, "dateadded":data[i].projects.dateadded, "location":data[i].projects.location, "status":0 }];
}
}
});
this.storage.set( 'projectsStore', this.project );
}
所以要澄清这是我想要实现的目标。让我们说用户执行项目3的归档功能,然后阵列需要看起来像这样(项目3的状态更新,但项目1和2没有更新)
[
{
"projects": {
"projectname": "one",
"dateadded": "16 April 2018, 7:23AM",
"status": 1
}
},
{
"projects": {
"projectname": "two",
"dateadded": "16 April 2018, 7:23AM",
"status": 1
}
},
{
"projects": {
"projectname": "three",
"dateadded": "16 April 2018, 7:32AM",
"status": 0
}
}
]
答案 0 :(得分:3)
如果您想更新status
=== projectname
的{{1}},您可以
parameter1
&#13;
答案 1 :(得分:0)
试试这个
function updateProject(projectName){
let toBeUpdated = projects.find(item => item.projects.projectname === projectName);
toBeUpdated && (toBeUpdated.projects.status = 0);
}
答案 2 :(得分:0)
public invokeEdit(event: MouseEvent): void {
console.log(event);
event.stopPropagation();
}
此结构可让您轻松更新任何项目的状态。拥有此结构后,您可以使用以下函数更新单个项目的状态:
[
{
"one": {
"dateadded": "16 April 2018, 7:23AM",
"status": 1
}
},
{
"two": {
"dateadded": "16 April 2018, 7:23AM",
"status": 1
}
},
{
"three": {
"dateadded": "16 April 2018, 7:32AM",
"status": 1
}
}
]