如何使用打字稿从对象数组中获取对象的一部分

时间:2020-03-13 06:42:04

标签: javascript arrays json angular typescript

我有一个对象数组,我只想过滤和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"
  } ]
}]

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

5 个答案:

答案 0 :(得分:4)

使用数组的查找方法如下。

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)

希望这会有所帮助!

答案 1 :(得分:1)

答案 2 :(得分:0)

如果我理解正确,请您解释 Array.prototype.filter将始终仅返回一个数组,您必须使用数组索引才能使对象脱离数组

答案 3 :(得分:0)

使用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

答案 4 :(得分:0)

您可以使用以下查找方法,该方法仅返回对象

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

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