以下是我的JSON对象:
$("#btn").on('click', function() {
var obj = {
"cells": [{
"type": "basic.Rect",
"position": {
"x": 320,
"y": 140
},
"size": {
"width": 90,
"height": 54
},
"angle": 0,
"id": "021d1aef-72c7-4bff-b605-054bb5652ee3",
"z": 1,
"attrs": {
"rect": {
"fill": "transparent",
"stroke": "#31d0c6",
"width": 50,
"height": 30,
"rx": 2,
"ry": 2,
"stroke-width": 2,
"stroke-dasharray": "0",
"id": "",
"compare": ""
},
"text": {
"fill": "#c6c7e2",
"text": "rect",
"font-size": 11,
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0
},
".": {
"data-tooltip-position": "left",
"data-tooltip-position-selector": ".joint-stencil"
}
}
}, {
"type": "basic.Rect",
"position": {
"x": 550,
"y": 120
},
"size": {
"width": 90,
"height": 54
},
"angle": 0,
"id": "b230e76b-aa4d-4cf7-8cee-16dc3161fc75",
"z": 2,
"attrs": {
"rect": {
"fill": "transparent",
"stroke": "#31d0c6",
"width": 50,
"height": 30,
"rx": 2,
"ry": 2,
"stroke-width": 2,
"stroke-dasharray": "0",
"id": "",
"compare": ""
},
"text": {
"fill": "#c6c7e2",
"text": "rect",
"font-size": 11,
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0
},
".": {
"data-tooltip-position": "left",
"data-tooltip-position-selector": ".joint-stencil"
}
}
}, {
"type": "basic.Text",
"position": {
"x": 320,
"y": 300
},
"size": {
"width": 90,
"height": 45
},
"angle": 0,
"id": "d692f3ff-8b17-4c8a-8c80-980e0e12047c",
"z": 3,
"attrs": {
"text": {
"font-size": 20,
"fill": "#7cbd31",
"text": "vrn",
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0,
"id": ""
}
}
}, {
"type": "basic.Text",
"position": {
"x": 460,
"y": 300
},
"size": {
"width": 90,
"height": 45
},
"angle": 0,
"id": "fb60eb9f-2f97-41dc-8b1c-4da6dffab9ab",
"z": 4,
"attrs": {
"text": {
"font-size": 20,
"fill": "#7cbd31",
"text": "vrnvalue",
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0,
"id": "vrn"
}
}
}]
};
我想找到具有id
attribute =“vrn”的节点,并将其text
属性值从vrvalue
更改为123
我试过了:
jQuery.each(obj, function(i,ob) {
jQuery.each(ob, function(i1,ob1) {
jQuery.each(ob1, function(i2,ob2) {
console.log(ob2);
});
});
但是,我现在感到迷茫。
答案 0 :(得分:0)
只需遍历多个单元格对象,如果id为vrn,则将其更改为123或其他内容。
if(obj.cells[i].attrs.text.id=='vrn'){
obj.cells[i].attrs.text.id='123';
}
var obj = {
"cells": [{
"type": "basic.Rect",
"position": {
"x": 320,
"y": 140
},
"size": {
"width": 90,
"height": 54
},
"angle": 0,
"id": "021d1aef-72c7-4bff-b605-054bb5652ee3",
"z": 1,
"attrs": {
"rect": {
"fill": "transparent",
"stroke": "#31d0c6",
"width": 50,
"height": 30,
"rx": 2,
"ry": 2,
"stroke-width": 2,
"stroke-dasharray": "0",
"id": "",
"compare": ""
},
"text": {
"fill": "#c6c7e2",
"text": "rect",
"font-size": 11,
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0
},
".": {
"data-tooltip-position": "left",
"data-tooltip-position-selector": ".joint-stencil"
}
}
}, {
"type": "basic.Rect",
"position": {
"x": 550,
"y": 120
},
"size": {
"width": 90,
"height": 54
},
"angle": 0,
"id": "b230e76b-aa4d-4cf7-8cee-16dc3161fc75",
"z": 2,
"attrs": {
"rect": {
"fill": "transparent",
"stroke": "#31d0c6",
"width": 50,
"height": 30,
"rx": 2,
"ry": 2,
"stroke-width": 2,
"stroke-dasharray": "0",
"id": "",
"compare": ""
},
"text": {
"fill": "#c6c7e2",
"text": "rect",
"font-size": 11,
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0
},
".": {
"data-tooltip-position": "left",
"data-tooltip-position-selector": ".joint-stencil"
}
}
}, {
"type": "basic.Text",
"position": {
"x": 320,
"y": 300
},
"size": {
"width": 90,
"height": 45
},
"angle": 0,
"id": "d692f3ff-8b17-4c8a-8c80-980e0e12047c",
"z": 3,
"attrs": {
"text": {
"font-size": 20,
"fill": "#7cbd31",
"text": "vrn",
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0,
"id": ""
}
}
}, {
"type": "basic.Text",
"position": {
"x": 460,
"y": 300
},
"size": {
"width": 90,
"height": 45
},
"angle": 0,
"id": "fb60eb9f-2f97-41dc-8b1c-4da6dffab9ab",
"z": 4,
"attrs": {
"text": {
"font-size": 20,
"fill": "#7cbd31",
"text": "vrnvalue",
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0,
"id": "vrn"
}
}
}]
};
var len=obj.cells.length;
for(i=0;i<len;i++){
if(obj.cells[i].attrs.text.id=='vrn'){
obj.cells[i].attrs.text.id='123';
}
}
console.log(obj);
答案 1 :(得分:0)
var cells = obj.cells.map(function (cell) {
if (cell.attrs.text.id === "vrn" ) {
cell.attrs.text.text = "123"
}
return cell
});
var obj = {
"cells": [{
"type": "basic.Rect",
"position": {
"x": 320,
"y": 140
},
"size": {
"width": 90,
"height": 54
},
"angle": 0,
"id": "021d1aef-72c7-4bff-b605-054bb5652ee3",
"z": 1,
"attrs": {
"rect": {
"fill": "transparent",
"stroke": "#31d0c6",
"width": 50,
"height": 30,
"rx": 2,
"ry": 2,
"stroke-width": 2,
"stroke-dasharray": "0",
"id": "",
"compare": ""
},
"text": {
"fill": "#c6c7e2",
"text": "rect",
"font-size": 11,
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0
},
".": {
"data-tooltip-position": "left",
"data-tooltip-position-selector": ".joint-stencil"
}
}
}, {
"type": "basic.Rect",
"position": {
"x": 550,
"y": 120
},
"size": {
"width": 90,
"height": 54
},
"angle": 0,
"id": "b230e76b-aa4d-4cf7-8cee-16dc3161fc75",
"z": 2,
"attrs": {
"rect": {
"fill": "transparent",
"stroke": "#31d0c6",
"width": 50,
"height": 30,
"rx": 2,
"ry": 2,
"stroke-width": 2,
"stroke-dasharray": "0",
"id": "",
"compare": ""
},
"text": {
"fill": "#c6c7e2",
"text": "rect",
"font-size": 11,
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0
},
".": {
"data-tooltip-position": "left",
"data-tooltip-position-selector": ".joint-stencil"
}
}
}, {
"type": "basic.Text",
"position": {
"x": 320,
"y": 300
},
"size": {
"width": 90,
"height": 45
},
"angle": 0,
"id": "d692f3ff-8b17-4c8a-8c80-980e0e12047c",
"z": 3,
"attrs": {
"text": {
"font-size": 20,
"fill": "#7cbd31",
"text": "vrn",
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0,
"id": ""
}
}
}, {
"type": "basic.Text",
"position": {
"x": 460,
"y": 300
},
"size": {
"width": 90,
"height": 45
},
"angle": 0,
"id": "fb60eb9f-2f97-41dc-8b1c-4da6dffab9ab",
"z": 4,
"attrs": {
"text": {
"font-size": 20,
"fill": "#7cbd31",
"text": "vrnvalue",
"font-family": "Roboto Condensed",
"font-weight": "Normal",
"stroke-width": 0,
"id": "vrn"
}
}
}]
};
var obj.cells = obj.cells.map(function (cell) {
if (cell.attrs.text.id === "vrn" ) {
cell.attrs.text.text = "123"
}
return cell
});
console.log(obj.cells[cells.length -1].attrs.text.text); // 123
答案 2 :(得分:-1)
试试这个
<tr></tr>