Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 操作JSON对象:如何引用和更新嵌套值_Javascript_Jquery_Json - Fatal编程技术网

Javascript 操作JSON对象:如何引用和更新嵌套值

Javascript 操作JSON对象:如何引用和更新嵌套值,javascript,jquery,json,Javascript,Jquery,Json,我只是刚刚开始,所以我很困惑我将如何处理以下内容 // so I believe this really isn't an object just a string representation, but for example please excuse the name var dataObj = "{'id': 1, 'data': { 'color': 'red', 'shape' : 'triang

我只是刚刚开始,所以我很困惑我将如何处理以下内容

// so I believe this really isn't an object just a string representation, but for example please excuse the name
var dataObj = "{'id': 1, 
                'data': { 'color': 'red', 
                          'shape' : 'triangle', 
                          'height' : 100, 
                          'width' : 45, 
                           },
                'id': 2, 
                'data': { 'color': 'blue', 
                          'shape' : 'square', 
                          'height' : 75, 
                          'width' : 67, 
                          },
                'id': 3, 
                'data': { 'color': 'orange', 
                          'shape' : 'circle', 
                          'height' : 89, 
                          'width' :24, 
                          }
                }";
因此,我遇到的问题是,如何通过id更新数据值的特定子集(比如SQL更新之类的东西)?javascript或jquery对我来说真的不重要,只是不知道这两者的方法

dataObjUpdate(2);    
function dataObjUpdate (passedID) {

    //access the data by the passedID match and update the color to black
}

感谢大家的帮助……

如果我们忽略我留下的评论,并假设您有一个JavaScript对象。我看到以下问题:

  • 您的ID在嵌套对象之外
  • 你正在使用一个对象,但你想要一个“列表”,你可以使用一个数组
下面是我自己构造对象的方式:

var data = [{ 
        color : 'red', 
        shape : 'triangle', 
        height : 100, 
        width : 45, 
        id:1
    },
    { 
        color: 'blue', 
        shape : 'square', 
        height : 75, 
        width : 67, 
        id: 2
    },
    {
        color: 'orange', 
        shape : 'circle', 
        height : 89, 
        width :24,
        id :3 
    }];
现在,我可以像您期望的那样使用以下命令查询它:

ES6有一个名为
find
的方法,它将
[0]
保存在末尾(表示第一个元素)

或者,您可以使用简单的for循环

var id3 = (function find(arr){
              for(var i=0;i<arr.length;i++){
                  if(arr[i].id === 3){
                      return arr[i];
                  }
              }
          })(data);
id3;
var id3=(函数查找(arr){

for(var i=0;iJSON是一种数据序列化格式(问题中字符串中的对象不是有效的JSON,JSON使用双引号)。您可以删除引号,然后得到一个有效的JavaScript对象。您的数据格式更重要的问题是,
id
属性在序列化对象中多次出现。我认为您可能希望更改为一个对象数组,而不是这一个对象。是的,这是有意义的……错误示例失败:).thanksA由id键控的字典也会生成(并且性能更高)。也就是说,如果id是唯一的,则这是一个有效点,在另一个对象(或映射)中索引id如果你存储了大量的数据,只支持现代浏览器,你也应该考虑索引数据库。更不用说如果IDS是连续的,你可以通过数组索引访问元素。谢谢本杰明!我很感激帮助/方向!我不太理解索引方法,但我想这是另一个Q。谢谢!欢迎贾斯廷!欢迎你接受这个答案,如果它解决了你的问题。我也建议JavaScript对象/数组章节中的雄辩的JavaScript或者在MDN中的对象的部分,它解释得很好。祝你好运!算出我的错误BTW.例子很好!再一次…谢谢!
var id3 = data.find(function(elem){
             return elem.id === 3;
          });
   id3;//the third object, with id 3
var id3 = (function find(arr){
              for(var i=0;i<arr.length;i++){
                  if(arr[i].id === 3){
                      return arr[i];
                  }
              }
          })(data);
id3;