[英]remove specific element from json
i have json
like 我有像
json
{
"type": "textbox",
"originX": "center",
"originY": "top",
"left": 1289.6,
"top": 695.43,
"width": 244.84,
"height": 36.72,
"fill": "rgb(0, 0, 0)",
"stroke": null,
"strokeWidth": 1,
"strokeDashArray": null,
"strokeLineCap": "butt",
"strokeLineJoin": "miter",
"strokeMiterLimit": 10,
"scaleX": 1,
"scaleY": 1,
"angle": 0,
"flipX": false,
"flipY": false,
"opacity": 1,
"shadow": null,
"visible": true,
"clipTo": null,
"backgroundColor": "",
"fillRule": "nonzero",
"globalCompositeOperation": "source-over",
"transformMatrix": null,
"lockMovementX": false,
"lockMovementY": false,
"evented": true,
"text": "FROM ONLY £199",
"fontSize": 25,
"fontWeight": "",
"fontFamily": "'trebuchet ms'",
"fontStyle": "",
"lineHeight": 1.3,
"textDecoration": "",
"textAlign": "left",
"textBackgroundColor": "",
"styles": {
"0": {
"1": {
"fontFamily": "'trebuchet ms'",
"fontSize": 25,
"fontWeight": "",
"fontStyle": ""
},
"2": {
"fontFamily": "'trebuchet ms'",
"fontSize": 25,
"fontWeight": "",
"fontStyle": ""
}
}
},
"minWidth": 20
}
You can see there is styles
attribute at second last, it has many other objects. 您可以看到倒数第二个有
styles
属性,它还有许多其他对象。 when you open ony one from that you will see fontFamily
, fontSize
fontWeight
. 当您从中打开一个时,您将看到
fontFamily
, fontSize
fontWeight
。 I want to remove fontWeight
from it. 我想从中删除
fontWeight
。 For this i tried this code 为此,我尝试了这段代码
if (typeof indexs['styles'] != "undefined") {
for (rowIndex in indexs['styles']) {
for (letterIndex in indexs['styles'][rowIndex]) {
if (indexs['styles'][rowIndex][letterIndex].fontWeight && indexs['styles'][rowIndex][letterIndex].fontWeight=='') {
delete indexs['styles'][rowIndex][letterIndex]['fontWeight'];
}
}
}
}
But not deleting that attribute. 但不能删除该属性。 Am i doing anything wrong there?
我在那里做错了吗?
You have to update this: 您必须更新此:
if (indexs['styles'][rowIndex][letterIndex].fontWeight=='') {
delete indexs['styles'][rowIndex][letterIndex]['fontWeight'];
}
because a blank value ""
is a falsy value and 因为空白值
""
是虚假值,
indexs['styles'][rowIndex][letterIndex].fontWeight
results as false
and &&
should work when both are true
. 结果为
false
和&&
都为true
时起作用。
try this too...to check whether the key "fontWeight" is there before checking for null value... 也尝试一下...在检查空值之前检查键“ fontWeight”是否存在...
if (typeof indexs['styles'] != "undefined") {
for (rowIndex in indexs['styles']) {
for (letterIndex in indexs['styles'][rowIndex]) {
if (indexs['styles'][rowIndex][letterIndex].hasOwnProperty("fontWeight") && indexs['styles'][rowIndex][letterIndex].fontWeight=='') {
delete indexs['styles'][rowIndex][letterIndex]['fontWeight'];
}
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.