-1

i have 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
}

You can see there is styles attribute at second last, it has many other objects. when you open ony one from that you will see fontFamily, fontSize fontWeight. I want to remove fontWeight from it. 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?

hemsbhardiya
  • 1,019
  • 11
  • 34

2 Answers2

1

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.

Jai
  • 71,335
  • 12
  • 70
  • 93
1

try this too...to check whether the key "fontWeight" is there before checking for null value...

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'];
            }
        }
    }   
}
});
Nofi
  • 1,647
  • 11
  • 18