简体   繁体   English

使用 javascript/jquery 访问嵌套 json 中未知父对象的子对象

[英]Access the child object of a unknown parent in nested json using javascript/jquery

I have a nested JSON object.我有一个嵌套的 JSON 对象。

I do not know the position of the parent in the nested JSON object.我不知道嵌套 JSON 对象中父对象的位置。

I know the property value of the parent I am trying to access.我知道我试图访问的父级的属性值。

I need to access the rows object whose index = 0 or 3 or 2 etc based on the items in an array.我需要根据数组中的项目访问索引 = 0 或 3 或 2 等的行对象。

If the array contains 0,3 then I would like to access the cells object of the rows object whose index property is 0 and then access the cells object of the rows object whose index property is 3.如果数组包含 0,3,那么我想访问索引属性为 0 的行对象的单元格对象,然后访问索引属性为 3 的行对象的单元格对象。

The topmost item in the rows object may not contain the index property as 0.行对象中最顶层的项目可能不包含索引属性为 0。

I won't know the position of the rows object although I know the property's value.尽管我知道属性的值,但我不知道行对象的位置。

Also i do not want to loop through the row collection using foreach.此外,我不想使用 foreach 遍历行集合。

I would like to know if there is a way to acheive without using loops.我想知道是否有一种方法可以在不使用循环的情况下实现。

I am trying to do this without loops and if not possible then i may have to consider using for loop /foreach as the last option.我正在尝试在没有循环的情况下执行此操作,如果不可能,那么我可能不得不考虑使用 for 循环 /foreach 作为最后一个选项。

data.rows[x].index=0
data.rows[y].index=3

here i do not know x and y but i know the index property value of data.rows[x] and data.rows[y]这里我不知道 x 和 y 但我知道 data.rows[x] 和 data.rows[y] 的索引属性值

var data ={
   "name":"Members",
   "rows":[
      {
         "index":3,
        "cells":[
            {
               "value":"Name",
               "color":"#000000",
               "enable":false,
               "index":0
            },
            {
               "value":"ID",
               "color":"#000000",
               "enable":false,
               "index":1
            },
            {
               "value":"Total",
               "color":"#000000",
               "enable":false,
               "index":2
            },
            {
               "value":"Dec2019_Data",
               "index":3
            },
            {
               "value":"Jan2020_Data",
               "index":4
            }
         ]
      },
      {
         "index":2,
         "cells":[
            {
               "value":"NewMember",
               "color":"#000000",
               "enable":false,
               "index":0
            },
            {
               "value":240,
               "color":"#000000",
               "enable":false,
               "index":1
            },
            {
               "value":200,
               "color":"#000000",
               "enable":false,
               "index":2
            },
            {
               "value":100,
               "color":"#000000",
               "enable":false,
               "index":3
            },
            {
               "value":100,
               "color":"#000000",
               "enable":false,
               "index":4
            }
         ]
      },
      {
         "index":0,
        "cells":[
            {
               "value":"Timo (718)",
               "color":"#000000",
               "enable":false,
               "index":0
            },
            {
               "value":150,
               "color":"#000000",
               "enable":false,
               "index":1
            },
            {
               "value":400,
               "color":"#000000",
               "enable":false,
               "index":2
            },
            {
               "value":300,
               "color":"#000000",
               "enable":false,
               "index":3
            },
            {
               "value":100,
               "color":"#000000",
               "enable":false,
               "index":4
            }
         ]
      },
      {
         "index":1,
         "cells":[
            {
               "color":"#000000",
               "enable":false,
               "index":0
            },
            {
               "color":"#000000",
               "enable":false,
               "index":1
            },
            {
               "color":"#000000",
               "enable":false,
               "index":2
            },
            {
               "color":"#000000",
               "enable":false,
               "index":3
            },
            {
               "color":"#000000",
               "enable":false,
               "index":4
            }
         ]
      }
   ]
}

i found a way to fetch the rows based on index我找到了一种基于索引获取行的方法

var jsonArray = [];
var rowCollection = data.rows;
var uniqRows=[0,3,2];
var ChangedRows=[];
ChangedRows= $.map(rowCollection, function(val, key) {
                                       if (uniqRows.indexOf(val.index) > -1)
                                           return val;
                                   });



ChangedRows.sort(function(a, b){
                                       return a.index-b.index
                                   })

var rowCount = uniqRows.length;

                                for (var i = 0; i < rowCount ; i++) {




                                    var objs=ChangedRows[i].cells;


                                        objs = objs.map(function (obj) {
                                            return { value: obj.value };
                                        });

                                        jsonArray.push(JSON.stringify(objs, null, 2));}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM