使用地图遍历对象数组。在javascript中过滤,减少或Object.keys()

时间:2018-01-15 11:34:03

标签: javascript json

我有一个JSON数组对象,我只想输出它的一些数据,就像一棵树,我尝试过不同的循环,但我想使用map,filter和reduce for是现在更好的遍历数组的方法对象。

我的预期输出就是这样。

enter image description here

注意:由于循环,树中的第二个Project101是错误的,它应该被移除并且树中只有Project101,Project102,Project103。

以下是json文件示例

Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2235)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    at org.hibernate.loader.Loader.list(Loader.java:2124)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
    ... 126 more
Caused by: org.postgresql.util.PSQLException: ERROR: column "btype" does not exist
  Hint: Perhaps you meant to reference the column "aclient0_.type".
  Position: 204
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:353)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
    at org.hibernate.loader.Loader.doQuery(Loader.java:697)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2232)

提前谢谢。

我尝试过的这个很长,因为循环。

  var jsonData = [
    {
        "id": 1,
        "project_name": "Project101",
        "updated_by": "Chaddilac Change",
        "created_date": "2018-01-09T15:49:54Z",
        "updated_date": "2018-01-09T15:49:54Z",
        "is_deleted": false,
        "languages": [
            {
                "id": 1,
                "language": "English",
                "created_by": "Sammy Sosa",
                "created_date": "2018-01-04T04:45:54Z",
                "beta_project": 1,
                "details": [
                    {
                        "id": 1,
                        "phase_name": "BetaPhase101",
                        "created_date": "2018-01-04T04:33:00Z",
                        "created_by": "John Doe",
                        "phase_title": "BetaTitle",
                        "external_url": "www.nba.com",
                        "segment": "Consumer",
                        "locale": "English",
                        "is_published": false,
                        "beta_start_date": "1975-01-01T00:00:00Z",
                        "beta_end_date": "1975-01-01T00:00:00Z",
                        "project_owner": "John Doe",
                        "project_subtitutes": "Gelly",
                        "last_update_date": "2018-01-04T04:33:00Z",
                        "last_updated_by": "John Doe",
                        "last_published_date": "2018-01-04T04:33:00Z",
                        "last_published_by": "Neil S",
                        "accrdn_title_one": "title one",
                        "accrdn_contents_one": "content one",
                        "accrdn_title_two": "title two",
                        "accrdn_contents_two": "content two",
                        "accrdn_title_three": "title three",
                        "accrdn_contents_three": "content three",
                        "accrdn_title_four": "titile four",
                        "accrdn_contents_four": "content four",
                        "accrdn_title_five": "title five",
                        "accrdn_contents_five": "content five",
                        "accrdn_title_six": "title six",
                        "accrdn_contents_six": "content six",
                        "beta_language": 1
                    },
                    {
                        "id": 2,
                        "phase_name": "BetaPhase102",
                        "created_date": "2010-10-10T00:00:00Z",
                        "created_by": "John Doe",
                        "phase_title": "BetaTitle",
                        "external_url": "www.nba.com",
                        "segment": "Consumer",
                        "locale": "English",
                        "is_published": false,
                        "beta_start_date": "2018-01-01T00:00:00Z",
                        "beta_end_date": "2018-10-20T00:00:00Z",
                        "project_owner": "John Doe",
                        "project_subtitutes": "Gelly",
                        "last_update_date": "2010-10-10T00:00:00Z",
                        "last_updated_by": "John Doe",
                        "last_published_date": "2010-10-10T00:00:00Z",
                        "last_published_by": "TRex",
                        "accrdn_title_one": "title one",
                        "accrdn_contents_one": "content one",
                        "accrdn_title_two": "title two",
                        "accrdn_contents_two": "content two",
                        "accrdn_title_three": "title three",
                        "accrdn_contents_three": "content three",
                        "accrdn_title_four": "titile four",
                        "accrdn_contents_four": "content four",
                        "accrdn_title_five": "title five",
                        "accrdn_contents_five": "content five",
                        "accrdn_title_six": "title six",
                        "accrdn_contents_six": "content six",
                        "beta_language": 1
                    }
                ]
            },
            {
                "id": 4,
                "language": "Japanese",
                "created_by": "John Doe",
                "created_date": "2018-01-09T01:35:23.743333Z",
                "beta_project": 1
            }
        ]
    },
    {
        "id": 2,
        "project_name": "Project102",
        "created_by": "Tsn",
        "updated_by": "Chad Change",
        "created_date": "2018-01-05T03:13:08Z",
        "updated_date": "2018-01-05T03:13:08Z",
        "is_deleted": false,
        "languages": [
            {
                "id": 2,
                "language": "Traditional Chines",
                "created_by": "John Doe",
                "created_date": "2018-01-04T04:46:16Z",
                "beta_project": 2,
                "details": [
                    {
                        "id": 4,
                        "phase_name": "BetaPhase103",
                        "created_date": "2018-01-04T04:37:13Z",
                        "created_by": "John Doe",
                        "phase_title": "BetaTitle",
                        "external_url": "www.nba.com",
                        "segment": "Consumer",
                        "locale": "English",
                        "is_published": false,
                        "beta_start_date": "2010-10-10T00:00:00Z",
                        "beta_end_date": "2018-01-01T00:00:00Z",
                        "project_owner": "John Doe",
                        "project_subtitutes": "Gelly",
                        "last_update_date": "2018-01-04T04:37:13Z",
                        "last_updated_by": "John Doe",
                        "last_published_date": "2018-01-04T04:37:13Z",
                        "last_published_by": "TRex",
                        "accrdn_title_one": "title one",
                        "accrdn_contents_one": "content one",
                        "accrdn_title_two": "title two",
                        "accrdn_contents_two": "content two",
                        "accrdn_title_three": "title three",
                        "accrdn_contents_three": "content three",
                        "accrdn_title_four": "titile four",
                        "accrdn_contents_four": "content four",
                        "accrdn_title_five": "title five",
                        "accrdn_contents_five": "content five",
                        "accrdn_title_six": "title six",
                        "accrdn_contents_six": "content six",
                        "beta_language": 2
                    }
                ]
            }
        ]
    },
    {
        "id": 10,
        "project_name": "Project103",
        "created_by": "Max",
        "updated_by": "Fried",
        "created_date": "2018-01-05T03:13:08Z",
        "updated_date": "2018-01-05T03:13:08Z",
        "is_deleted": false,
        "languages": [
            {
                "id": 3,
                "language": "Simplified Chines",
                "created_by": "John Doe",
                "created_date": "2018-01-09T01:34:26.240000Z",
                "beta_project": 10
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

看到这个,

https://jsfiddle.net/xianshenglu/5wtLffjp/1/

核心代码:

function transData(data) {
 let dataTemp = JSON.parse(JSON.stringify(data));

 function iteratorProperty(dataTemp, propertyArray, level) {
     if (dataTemp != undefined) {
         dataTemp.forEach((obj, index) => {
             Object.keys(obj).forEach((key, val) => {
                 if (propertyArray.indexOf(key) === -1) {
                     delete obj[key];
                 }
             });
             switch (level) {
                 case 0:
                     iteratorProperty(obj.languages, ['language', 'details'], 1);
                     break;
                 case 1:
                     iteratorProperty(obj.details, ['phase_name'], 2);
                     break;
             }
         });
     }
     return dataTemp;
 }

 iteratorProperty(dataTemp, ['project_name', 'languages'], 0);

 return dataTemp;
}