【问题标题】:How to get part of Object from Array of Objects using typescript如何使用打字稿从对象数组中获取对象的一部分
【发布时间】:2020-03-13 06:42:04
【问题描述】:

我有一个对象数组,我想过滤和 ftech 仅对象的一部分,我将作为主体参数发送给 API。 下面是我的代码,但是它返回给我一个数组,并且在里面我有我的对象。我不希望它是一个数组,而只是一个对象。 我怎样才能做到这一点。

原始对象数组:

[ {
"flowId" : 11,
"flowName" : "jobtest003",
"version" : 1,
"ingestionFlowId" : "",
"jobCreatedDate" : "25-05-2020",
"jobUpdateDate" : "28-06-2020",
"jobLastRunDate" : "29-06-2020",
"active" : false,
"properties" : [ {
"id" : 12,
"key" : "sourceTable",
"value" : "job002",
"category" : "General Settings"
}, {
"id" : 13,
"key" : "Source",
"value" : "api",
"category" : "Source Properties"
},  {
"id" : 147,
"key" : "Target Path",
"value" : "/raw/au/jackson",
"category" : "Destination Properties"
} ]
}, {
"flowId" : 21,
"flowName" : "jobtest004",
"version" : 1,
"ingestionFlowId" : null,
"jobCreatedDate" : "25-05-2020",
"jobUpdateDate" : "28-06-2020",
"jobLastRunDate" : "29-06-2020",
"active" : false,
"properties" : [ {
"id" : 21,
"key" : "sourceTable",
"value" : "job003",
"category" : "General Settings"
}, {
"id" : 22,
"key" : "Source",
"value" : "api",
"category" : "Source Properties"
}, {
"id" : 23,
"key" : "Client ID",
"value" : "ebf73456-443e-4986-941b-057906d25e2f",
"category" : "Destination Properties"
},  {
"id" : 147,
"key" : "Target Path",
"value" : "/raw/au/jackson",
"category" : "Destination Properties"
} ]
}, {
"flowId" : 22,
"flowName" : "jobtest004",
"version" : 1,
"ingestionFlowId" : null,
"jobCreatedDate" : "25-05-2020",
"jobUpdateDate" : "28-06-2020",
"jobLastRunDate" : "29-06-2020",
"active" : false,
"properties" : [ {
"id" : 21,
"key" : "sourceTable",
"value" : "job003",
"category" : "General Settings"
}, {
"id" : 22,
"key" : "Source",
"value" : "api",
"category" : "Source Properties"
}, {
"id" : 23,
"key" : "Client ID",
"value" : "ebf73456-443e-4986-941b-057906d25e2f",
"category" : "Destination Properties"
},  {
"id" : 147,
"key" : "Target Path",
"value" : "/raw/au/jackson",
"category" : "Destination Properties"
} ]
} ]

我想要的结果:

{
  "flowId" : 20,
  "flowName" : "jobtest004",
  "version" : 1,
  "ingestionFlowId" : null,
  "jobCreatedDate" : "25-05-2020",
  "jobUpdateDate" : "28-06-2020",
  "jobLastRunDate" : "29-06-2020",
  "active" : false,
  "properties" : [ {
    "id" : 21,
    "key" : "sourceTable",
    "value" : "job003",
    "category" : "General Settings"
  }, {
    "id" : 22,
    "key" : "Source",
    "value" : "api",
    "category" : "Source Properties"
  }, {
    "id" : 23,
    "key" : "Client ID",
    "value" : "ebf73456-443e-4986-941b-057906d25e2f",
    "category" : "Destination Properties"
  },  {
    "id" : 147,
    "key" : "Target Path",
    "value" : "/raw/au/jackson",
    "category" : "Destination Properties"
  } ]
}

我得到的结果:

[{
  "flowId" : 20,
  "flowName" : "jobtest004",
  "version" : 1,
  "ingestionFlowId" : null,
  "jobCreatedDate" : "25-05-2020",
  "jobUpdateDate" : "28-06-2020",
  "jobLastRunDate" : "29-06-2020",
  "active" : false,
  "properties" : [ {
    "id" : 21,
    "key" : "sourceTable",
    "value" : "job003",
    "category" : "General Settings"
  }, {
    "id" : 22,
    "key" : "Source",
    "value" : "api",
    "category" : "Source Properties"
  }, {
    "id" : 23,
    "key" : "Client ID",
    "value" : "ebf73456-443e-4986-941b-057906d25e2f",
    "category" : "Destination Properties"
  },  {
    "id" : 147,
    "key" : "Target Path",
    "value" : "/raw/au/jackson",
    "category" : "Destination Properties"
  } ]
}]

我不希望我的结果是一个数组,而我只是希望它是一个对象。

【问题讨论】:

    标签: javascript arrays json angular typescript


    【解决方案1】:

    使用数组的find方法如下。

    var array = your data;
    
    var object_needed = array.find(d=>d.flowId == 21)//use required id in place of 21 or use required property in place of .flowIf
    //console.log(object_needed)
    

    希望对你有帮助!!!

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 谢谢。我通过循环和过滤数组使我的任务复杂化了。查找很简单而且效果很好。
      【解决方案3】:

      如果我理解正确,请提供您的解释 Array.prototype.filter 将始终只返回一个数组,您必须使用数组索引将对象从数组中取出

      【讨论】:

        【解决方案4】:

        使用reduce方法将数组转换为对象。

        const convertArrayToObject = (array, key) => {
              const initialValue = {};
              return array.reduce((obj, item) => {
                return {
                  ...obj,
                  [item[key]]: item,
                };
              }, initialValue);
            };
        
        let testData = [ { "flowId" : 11, "flowName" : "jobtest003", "version" : 1, "ingestionFlowId" : "", "jobCreatedDate" : "25-05-2020", "jobUpdateDate" : "28-06-2020", "jobLastRunDate" : "29-06-2020", "active" : false, "properties" : [ { "id" : 12, "key" : "sourceTable", "value" : "job002", "category" : "General Settings" }, { "id" : 13, "key" : "Source", "value" : "api", "category" : "Source Properties" }, { "id" : 147, "key" : "Target Path", "value" : "/raw/au/jackson", "category" : "Destination Properties" } ] }, { "flowId" : 21, "flowName" : "jobtest004", "version" : 1, "ingestionFlowId" : null, "jobCreatedDate" : "25-05-2020", "jobUpdateDate" : "28-06-2020", "jobLastRunDate" : "29-06-2020", "active" : false, "properties" : [ { "id" : 21, "key" : "sourceTable", "value" : "job003", "category" : "General Settings" }, { "id" : 22, "key" : "Source", "value" : "api", "category" : "Source Properties" }, { "id" : 23, "key" : "Client ID", "value" : "ebf73456-443e-4986-941b-057906d25e2f", "category" : "Destination Properties" }, { "id" : 147, "key" : "Target Path", "value" : "/raw/au/jackson", "category" : "Destination Properties" } ] }, { "flowId" : 22, "flowName" : "jobtest004", "version" : 1, "ingestionFlowId" : null, "jobCreatedDate" : "25-05-2020", "jobUpdateDate" : "28-06-2020", "jobLastRunDate" : "29-06-2020", "active" : false, "properties" : [ { "id" : 21, "key" : "sourceTable", "value" : "job003", "category" : "General Settings" }, { "id" : 22, "key" : "Source", "value" : "api", "category" : "Source Properties" }, { "id" : 23, "key" : "Client ID", "value" : "ebf73456-443e-4986-941b-057906d25e2f", "category" : "Destination Properties" }, { "id" : 147, "key" : "Target Path", "value" : "/raw/au/jackson", "category" : "Destination Properties" } ] } ];
        
            console.log(
              convertArrayToObject(testData,'flowId')
            );

        click here for detailed explanation

        【讨论】:

          【解决方案5】:

          你可以使用下面的 find 方法,它只返回对象

          arrayOfData.find(function(obj){
          return obj.Id == 11
          })
          

          arrayOfData.find(obj => obj.Id == 11)
          

          【讨论】:

            猜你喜欢
            • 2021-11-02
            • 2020-10-05
            • 1970-01-01
            • 1970-01-01
            • 2019-05-17
            • 2018-07-03
            • 1970-01-01
            • 2022-01-12
            • 1970-01-01
            相关资源
            最近更新 更多