javascript 循环遍历数组并筛选出特定对象的函数

xurqigkl  于 2023-03-11  发布在  Java
关注(0)|答案(2)|浏览(150)

我有一个json,基于这个json我必须创建一个表。
每个“行”都是表中的实际行,每个“子行”都是一个指示符,指示主行下面有一个可折叠行,依此类推。
每一行都有一个删除函数,该行作为一个参数加上主数组给予。
我需要一个函数来迭代所有对象和所有嵌套对象,以匹配特定的行并将其过滤掉。

{
   "row":{
      "property1":"1",
      "property2":"2"
   },
   "sub_row":{
      "first_extra":{
         "records":[
            {
               "row":{
                  "property1":"1",
                  "property2":"2",
                  "property3":"3"
               },
               "sub_row":{
                  "second_extra":{
                     "records":[
                        {
                           "row":{
                              "property1":"1",
                              "property2":"2",
                              "property3":"3"
                           },
                           "sub_row":{
                              
                           }
                        },
                        {
                           "row":{
                              "property1":"2470",
                              "property2":"1043",
                              "property3":"546 765-7237"
                           },
                           "sub_row":{
                              
                           }
                        }
                     ]
                  }
               }
            }
         ]
      }
   }
}

例如,我想删除此部件:

"row":{
                                  "property1":"2470",
                                  "property2":"1043",
                                  "property3":"546 765-7237"
                               },

尝试多个功能,但似乎均无效

sd2nnvve

sd2nnvve1#

首先,如果您有原始数据,则将其移动到data.json文件。

let data;
    // get the data from the file
    fetch("data.json")
      .then((response) => response.json())
      .then((result) => {
        // set the filtered data to the data.
        data = filterRowById(result, "row");
        console.log(data);
      })
      .catch((error) => console.error(error));
    
    function filterRowById(jsonData, rowId) {
      const filteredData = { ...jsonData };
    
      // delete the data you want to delete with delete method
      delete filteredData[rowId];
    
      return filteredData;
    }

检查沙箱:https://codesandbox.io/embed/hardcore-mirzakhani-cvj6hg?fontsize=14&hidenavigation=1&theme=dark

nzrxty8p

nzrxty8p2#

我希望这就是你要问的,移除与嵌套对象值匹配的对象。

function filterTree(data, row2delete) {
  return Object.entries(data).reduce((acc, [key, value]) => {
    if (key === "row") {
      if (!isSameRow(row2delete, value)) {
        acc[key] = value;
      }
    }
    
    if (Array.isArray(value)) {
      const subValues = value.map(el => filterTree(el ,row2delete));
      acc[key] = subValues;
    }
    
    if (key === "sub_row" || key.endsWith("_extra")) {
      acc[key] = filterTree(value, row2delete);
    }
    
    return acc;
  }, {});
}

function isSameRow(obj1, obj2) {
  for (const key in obj1) {
    const value = obj1[key];
    if (key && obj2[key] && obj2[key] === value) {
      continue;
    } else {
      return false;
    }
  }
  
  return true;
}

console.log(filterTree(data, {"property1":"1","property2":"2","property3":"3"}))

如果你的输入类型是一个数组,那么你可以用map来 Package 它:

function filter(values, row2delete) {
  return values.map(item => filterTree(item, row2delete));
}

console.log(filter(data, {"property1":"1","property2":"2","property3":"3"}))

相关问题