Javascript 查找JSON对象中的节点,其中;X";属性等于;xyz";

Javascript 查找JSON对象中的节点,其中;X";属性等于;xyz";,javascript,jquery,json,Javascript,Jquery,Json,以下是我的JSON对象: $("#btn").on('click', function() { var obj = { "cells": [{ "type": "basic.Rect", "position": { "x": 320, "y": 140 }, "size": { "width": 90, "height": 54 }, "angle"

以下是我的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);
  });
  });

但是,我现在感到失落

只需遍历多个cells对象,如果id为vrn,则将其更改为123或其他内容

if(obj.cells[i].attrs.text.id=='vrn'){
   obj.cells[i].attrs.text.id='123';
}
var obj={
“细胞”:[{
“类型”:“basic.Rect”,
“职位”:{
“x”:320,
“y”:140
},
“尺寸”:{
“宽度”:90,
“高度”:54
},
“角度”:0,
“id”:“021d1aef-72c7-4bff-b605-054bb5652ee3”,
“z”:1,
“属性”:{
“rect”:{
“填充”:“透明”,
“笔划”:“31d0c6”,
“宽度”:50,
“高度”:30,
“rx”:2,
“ry”:2,
“笔划宽度”:2,
“笔划数组”:“0”,
“id”:“,
“比较”:”
},
“文本”:{
“填充”:“c6c7e2”,
“文本”:“rect”,
“字体大小”:11,
“字体系列”:“Roboto Condensed”,
“字体重量”:“正常”,
“笔划宽度”:0
},
".": {
“数据工具提示位置”:“左”,
“数据工具提示位置选择器”:“.joint模具”
}
}
}, {
“类型”:“basic.Rect”,
“职位”:{
“x”:550,
“y”:120
},
“尺寸”:{
“宽度”:90,
“高度”:54
},
“角度”:0,
“id”:“b230e76b-aa4d-4cf7-8cee-16dc3161fc75”,
“z”:2,
“属性”:{
“rect”:{
“填充”:“透明”,
“笔划”:“31d0c6”,
“宽度”:50,
“高度”:30,
“rx”:2,
“ry”:2,
“笔划宽度”:2,
“笔划数组”:“0”,
“id”:“,
“比较”:”
},
“文本”:{
“填充”:“c6c7e2”,
“文本”:“rect”,
“字体大小”:11,
“字体系列”:“Roboto Condensed”,
“字体重量”:“正常”,
“笔划宽度”:0
},
".": {
“数据工具提示位置”:“左”,
“数据工具提示位置选择器”:“.joint模具”
}
}
}, {
“类型”:“basic.Text”,
“职位”:{
“x”:320,
“y”:300
},
“尺寸”:{
“宽度”:90,
“高度”:45
},
“角度”:0,
“id”:“d692f3ff-8b17-4c8a-8c80-980e0e12047c”,
“z”:3,
“属性”:{
“文本”:{
“字体大小”:20,
“填充”:“7cbd31”,
“文本”:“vrn”,
“字体系列”:“Roboto Condensed”,
“字体重量”:“正常”,
“笔划宽度”:0,
“id”:”
}
}
}, {
“类型”:“basic.Text”,
“职位”:{
“x”:460,
“y”:300
},
“尺寸”:{
“宽度”:90,
“高度”:45
},
“角度”:0,
“id”:“fb60eb9f-2f97-41dc-8b1c-4da6dffab9ab”,
“z”:4,
“属性”:{
“文本”:{
“字体大小”:20,
“填充”:“7cbd31”,
“文本”:“vrnvalue”,
“字体系列”:“Roboto Condensed”,
“字体重量”:“正常”,
“笔划宽度”:0,
“id”:“vrn”
}
}
}]
};
var len=obj.cells.length;

对于(i=0;i试试这个

obj.cells.filter(function(rm){if(rm.attrs.text.id == "vrn"){
     return rm.attrs.text.text = 123
} })

console.log(obj)
使用

var obj={
“细胞”:[{
“类型”:“basic.Rect”,
“职位”:{
“x”:320,
“y”:140
},
“尺寸”:{
“宽度”:90,
“高度”:54
},
“角度”:0,
“id”:“021d1aef-72c7-4bff-b605-054bb5652ee3”,
“z”:1,
“属性”:{
“rect”:{
“填充”:“透明”,
“笔划”:“31d0c6”,
“宽度”:50,
“高度”:30,
“rx”:2,
“ry”:2,
“笔划宽度”:2,
“笔划数组”:“0”,
“id”:“,
“比较”:”
},
“文本”:{
“填充”:“c6c7e2”,
“文本”:“rect”,
“字体大小”:11,
“字体系列”:“Roboto Condensed”,
“字体重量”:“正常”,
“笔划宽度”:0
},
".": {
“数据工具提示位置”:“左”,
“数据工具提示位置选择器”:“.joint模具”
}
}
}, {
“类型”:“basic.Rect”,
“职位”:{
“x”:550,
“y”:120
},
“尺寸”:{
“宽度”:90,
“高度”:54
},
“角度”:0,
“id”:“b230e76b-aa4d-4cf7-8cee-16dc3161fc75”,
“z”:2,
“属性”:{
“rect”:{
“填充”:“透明”,
“笔划”:“31d0c6”,
“宽度”:50,
“高度”:30,
“rx”:2,
“ry”:2,
“笔划宽度”:2,
“笔划数组”:“0”,
“id”:“,
“比较”:”
},
“文本”:{
“填充”:“c6c7e2”,
“文本”:“rect”,
“字体大小”:11,
“字体系列”:“Roboto Condensed”,
“字体重量”:“正常”,
“笔划宽度”:0
},
".": {
“数据工具提示位置”:“左”,
“数据工具提示位置选择器”:“.joint模具”
}
}
}, {
“类型”:“basic.Text”,
“职位”:{
“x”:320,
“y”:300
},
“尺寸”:{
“宽度”:90,
“高度”:45
},
“角度”:0,
“id”:“d692f3ff-8b17-4c8a-8c80-980e0e12047c”,
“z”:3,
“属性”:{
“文本”:{
“字体大小”:20,
"
var cells = obj.cells.map(function (cell) {
    if (cell.attrs.text.id === "vrn" ) {
        cell.attrs.text.text = "123"
    }
    return cell
});