更新对象 JSON 通过 Javascript

如何动态更新以下对象 JSON 通过 javascript 或者 Jquery?


var jsonObj = [{'Id':'1','Username':'Ray','FatherName':'Thompson'}, 
{'Id':'2','Username':'Steve','FatherName':'Johnson'},
{'Id':'3','Username':'Albert','FatherName':'Einstein'}]


我想动态更新用户名 'Thomas', 在哪里 'Id' - 这是 '3'.

我怎样才能实现这一目标?
已邀请:

裸奔

赞同来自:

一个简单的解决方案 JavaScript, 强加出来
jsonObj

已经包含了 JSON:

上面的循环搜索相应的标识符,设置适当的用户名和
break

从循环发生变化后:


for /var i = 0; i < jsonObj.length; i++/ {
if /jsonObj[i].Id === 3/ {
jsonObj[i].Username = "Thomas";
break;
}
}


http://jsfiddle.net/cAyRs/1/
以下是函数中最包装的相同:


function setUsername/id, newUsername/ {
for /var i = 0; i < jsonObj.length; i++/ {
if /jsonObj[i].Id === id/ {
jsonObj[i].Username = newUsername;
return;
}
}
}

// Call as
setUsername/3, "Thomas"/;

石油百科

赞同来自:

只需重复列表,然后检查每个对象的属性。


for /var i = 0; i < jsonObj.length; ++i/ {
if /jsonObj[i]['Id'] === '3'/ {
jsonObj[i]['Username'] = 'Thomas';
}
}

二哥

赞同来自:

$/document/.ready/function//{ 
var jsonObj = [{'Id':'1','Username':'Ray','FatherName':'Thompson'},
{'Id':'2','Username':'Steve','FatherName':'Johnson'},
{'Id':'3','Username':'Albert','FatherName':'Einstein'}];

$.each/jsonObj,function/i,v/{
if /v.Id == 3/ {
v.Username = "Thomas";
return false;
}
}/;

alert/"New Username: " + jsonObj[2].Username/;

}/;

冰洋

赞同来自:

使用:


var parsedobj = jQuery.parseJSON/ jsonObj/;


只有当您不需要留在字符串中时,这将是有用的。
否则,您必须将其转换为 JSON, 使用库 JSON.

石油百科

赞同来自:

var i = jsonObj.length;
while / i --> 0 / {
if / jsonObj[i].Id === 3 / {
jsonObj[ i ].Username = 'Thomas';
break;
}
}


或者,如果阵列始终订购 IDs:


jsonObj[ 2 ].Username = 'Thomas';

冰洋

赞同来自:

JSON - 这是

物体的名称

JavaScript . 这样的东西

一个东西 JSON

, 不存在 . JSON只是代表一个对象的方法 JavaScript 在文中。

因此,您正在寻找一种更新对象的方法 in in-memory JavaScript.

展示如何更简单。

风见雨下

赞同来自:

我做了答案

Berkovsky在步骤 /或两者/ 此外,并创建了更灵活的功能,允许您使用任何搜索字段和任何目标字段。 对于娱乐,我有机会 splat / * /, 对某人可能想要为每个人提供替代品。

jQuery 必要的 NOT.

checkAllRows 允许您中断搜索找到性能的参数或更换先前提到的所有内容。


function setVal/update/ {
/* Included to show an option if you care to use jQuery
var defaults = { jsonRS: null, lookupField: null, lookupKey: null,
targetField: null, targetData: null, checkAllRows: false };
//update = $.extend/{}, defaults, update/; */

for /var i = 0; i < update.jsonRS.length; i++/ {
if /update.jsonRS[i][update.lookupField] === update.lookupKey || update.lookupKey === '*'/ {
update.jsonRS[i][update.targetField] = update.targetData;
if /!update.checkAllRows/ { return; }
}
}
}


var jsonObj = [{'Id':'1','Username':'Ray','FatherName':'Thompson'},
{'Id':'2','Username':'Steve','FatherName':'Johnson'},
{'Id':'3','Username':'Albert','FatherName':'Einstein'}]


使用您的数据,您将使用:


var update = {
jsonRS: jsonObj,
lookupField: "Id",
lookupKey: 2,
targetField: "Username",
targetData: "Thomas",
checkAllRows: false
};

setVal/update/;


和鲍勃 - 你的叔叔。 :/ [完美工作]

帅驴

赞同来自:

例如,我在篮子的功能中使用这种技术。

让我们将新产品添加到篮子里。


var productArray=[];


$/document/.on/'click','[cartBtn]',function/e/{
e.preventDefault//;
$/this/.html/'<i class="fa fa-check"></i>Added to cart'/;
console.log/'Item added '/;
var productJSON={"id":$/this/.attr/'pr_id'/, "nameEn":$/this/.attr/'pr_name_en'/, "price":$/this/.attr/'pr_price'/, "image":$/this/.attr/'pr_image'/, "quantity":1, "discount":0, "total":$/this/.attr/'pr_price'/};


if/localStorage.getObj/'product'/!==null/{
productArray=localStorage.getObj/'product'/;
productArray.push/productJSON/;
localStorage.setObj/'product', productArray/;
}
else{
productArray.push/productJSON/;
localStorage.setObj/'product', productArray/;
}


itemCountInCart/productArray.length/;

}/;


在向篮子添加某些项目后,会生成数组 json 像那样


[
{
"id": "95",
"nameEn": "New Braslet",
"price": "8776",
"image": "1462012394815.jpeg",
"quantity": 1,
"discount": 0,
"total": "8776"
},
{
"id": "96",
"nameEn": "new braslet",
"price": "76",
"image": "1462012431497.jpeg",
"quantity": 1,
"discount": 0,
"total": "76"
},
{
"id": "97",
"nameEn": "khjk",
"price": "87",
"image": "1462012483421.jpeg",
"quantity": 1,
"discount": 0,
"total": "87"
}
]


从篮子里删除某种物品。


$/document/.on/'click','[itemRemoveBtn]',function//{

var arrayFromLocal=localStorage.getObj/'product'/;
findAndRemove/arrayFromLocal,"id",$/this/.attr/'basketproductid'//;
localStorage.setObj/'product', arrayFromLocal/;
loadBasketFromLocalStorageAndRender//;
}/;

//This function will remove element by specified property. In my case this is ID.
function findAndRemove/array, property, value/ {
array.forEach/function/result, index/ {
if/result[property] === value/ {
//Remove from array
console.log/'Removed from index is '+index+' result is '+JSON.stringify/result//;
array.splice/index, 1/;

}
}/;
}


最后,这个问题的真实答案 "Updating a JSON object using JS". 在我的示例中,在更改元素的值时更新产品数量和总价格 "number".


$/document/.on/'keyup mouseup','input[type=number]',function//{

var arrayFromLocal=localStorage.getObj/'product'/;
setQuantityAndTotalPrice/arrayFromLocal,$/this/.attr/'updateItemid'/,$/this/.val///;
localStorage.setObj/'product', arrayFromLocal/;
loadBasketFromLocalStorageAndRender//;
}/;

function setQuantityAndTotalPrice/array,id,quantity/ {

array.forEach/function/result, index/ {
if/result.id === id/ {
result.quantity=quantity;
result.total=/quantity*result.price/;
}
}/;

}

三叔

赞同来自:

var jsonObj = [{'Id':'1','Quantity':'2','Done':'0','state':'todo',
'product_id':[315,"[LBI-W-SL-3-AG-TA004-C650-36] LAURA BONELLI-WOMEN'S-SANDAL"],
'Username':'Ray','FatherName':'Thompson'},
{'Id':'2','Quantity':'2','Done':'0','state':'todo',
'product_id':[314,"[LBI-W-SL-3-AG-TA004-C650-36] LAURA BONELLI-WOMEN'S-SANDAL"],
'Username':'Steve','FatherName':'Johnson'},
{'Id':'3','Quantity':'2','Done':'0','state':'todo',
'product_id':[316,"[LBI-W-SL-3-AG-TA004-C650-36] LAURA BONELLI-WOMEN'S-SANDAL"],
'Username':'Albert','FatherName':'Einstein'}];

for /var i = 0; i < jsonObj.length; ++i/ {
if /jsonObj[i]['product_id'][0] === 314/ {


this.onemorecartonsamenumber//;


jsonObj[i]['Done'] = ""+this.quantity_done+"";

if/jsonObj[i]['Quantity'] === jsonObj[i]['Done']/{
console.log/'both are equal'/;
jsonObj[i]['state'] = 'packed';
}else{
console.log/'not equal'/;
jsonObj[i]['state'] = 'todo';
}

console.log/'quantiy',jsonObj[i]['Quantity']/;
console.log/'done',jsonObj[i]['Done']/;


}
}

console.log/'final',jsonObj/;
}

quantity_done: any = 0;

onemorecartonsamenumber// {
this.quantity_done += 1;
console.log/this.quantity_done + 1/;
}

要回复问题请先登录注册