以下是我的JSON
[{ "id": "1",
"name" : "rob",
"Lastname":"Xyz"
},
{ "id": "2",
"name" : "xyz",
"Lastname":"abc"
}]
我有一个表单,用户将输入他的名字和姓氏,我想在这里实现的是检查并查看用户输入信息是否在JSON中可用。如果是用用户信息更新,则使用.push()
添加新对象答案 0 :(得分:1)
也许这会对你有所帮助:
export class Test {
// Objects array
myObjects = [{ "id": "1",
"name" : "rob",
"Lastname":"Xyz"
},
{ "id": "2",
"name" : "xyz",
"Lastname":"abc"
}]
// Method responsible for finding an object who has the name passed in as a parameter
getIndexByName(name: string): number{
let index: number;
this.myObjects.forEach(object => {
if(object.name == name){ // Do your filtering here
index = this.myObjects.indexOf(object);
}
})
return index;
}
updateObject(obj: any){
let index = this.getIndexByName(obj.name);
if(index){
this.myObjects.splice(index,1,obj); // This is the usual function that I use when I find myself needing to 'update' something in an array
}
}
}
你可以像这样使用它:
let user = {"id": "2", "name" : "xyz", "Lastname":"yjk"};
this.updateObject(user);
答案 1 :(得分:0)
var response = await Policies.PolicyWrap.ExecuteAsync(
ct => _httpClient.PostAsync(/* uri */, new StringContent(request), ct),
CancellationToken.None
);