Javascript 从json中删除特定元素
我有Javascript 从json中删除特定元素,javascript,json,Javascript,Json,我有jsonlike { "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, "stroke
json
like
{
"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
}
您可以在最后一秒看到styles
属性,它有许多其他对象。当您从中打开一个时,您将看到fontFamily
,fontSize
fontWeight
。我想从中删除fontWeight
。为此,我尝试了这个代码
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'];
}
}
}
}
但不删除该属性。我做错什么了吗?您必须更新以下内容:
if (indexs['styles'][rowIndex][letterIndex].fontWeight=='') {
delete indexs['styles'][rowIndex][letterIndex]['fontWeight'];
}
因为空白值“
是错误的值
indexs['styles'][rowIndex][letterIndex].fontWeight
当
为真时,为假
和为假
的结果应起作用也尝试此操作…在检查空值之前检查键“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'];
}
}
}
}
});
不要链接到内容,请将其包含在问题中(可能重复)。你也可以检查一下:@SampoSarrala我试过那个代码,但对我不起作用,这就是为什么我在这里发布这个问题。同样,不要链接到内容-在@Amit no content中阅读所有关于它的内容。现在想象