如何从响应(带有数组的对象)获取数据数组?

时间:2018-08-28 09:32:06

标签: javascript jquery

我正在从这样的服务中得到回复

var obj = {
  "master": [
      {
        "id": 1,
        "abc": [
          {
            "id": 1,
            "categoryId": 1,
            "displayName": "Prepaid",
            "value": "PREPAID"
          },
          {
            "id": 2,
            "categoryId": 1,
            "displayName": "Prepaid CYN",
            "value": "PREPAID_CYN"
          }
        ],
        "name": "Product Type",
        "value": "productType"
      },
      {
        "id": 2,
        "abc": [
          {
            "id": 6,
            "categoryId": 2,
            "displayName": "Mobile",
            "value": "Mobile"
          }
        ],
        "name": "Criteria",
        "value": "criteria"
      },
      {
        "id": 3,
        "abc": [
          {
            "id": 7,
            "categoryId": 3,
            "displayName": "Card",
            "value": "Aasssar"
          },
          {
            "id": 8,
            "categoryId": 3,
            "displayName": "Driving",
            "value": "li"
          }
        ],
        "name": "Proof",
        "value": "Proof"
      }     
    ]
}

let proofArr=[],
productType=[];

for(var i=0;obj.master.length;i++){
  console.log(obj)
  if(obj[i].master[i].value ==='productType'){
productType = obj[i].master[i].abc;
  }
}

for(var i=0;obj.master.length;i++){
  if(obj[i].master[i].value ==='product type'){
proofArr = obj[i].master[i].abc;
  }
}

console.log(productType)
console.log(proofArr)

我想转换或从响应中获取数组。

预期输出

productType =

[
          {
            "id": 1,
            "categoryId": 1,
            "displayName": "Prepaid",
            "value": "PREPAID"
          },
          {
            "id": 2,
            "categoryId": 1,
            "displayName": "Prepaid CYN",
            "value": "PREPAID_CYN"
          }
        ]

proofArr =

[
          {
            "id": 7,
            "categoryId": 3,
            "displayName": "Card",
            "value": "Aasssar"
          },
          {
            "id": 8,
            "categoryId": 3,
            "displayName": "Driving",
            "value": "li"
          }
        ]

我将如何从响应中获取数据?

3 个答案:

答案 0 :(得分:3)

您还有另一个选择,就是使用find()搜索第一个匹配项。

var obj = {"master":[{"id":1,"abc":[{"id":1,"categoryId":1,"displayName":"Prepaid","value":"PREPAID"},{"id":2,"categoryId":1,"displayName":"Prepaid CYN","value":"PREPAID_CYN"}],"name":"Product Type","value":"productType"},{"id":2,"abc":[{"id":6,"categoryId":2,"displayName":"Mobile","value":"Mobile"}],"name":"Criteria","value":"criteria"},{"id":3,"abc":[{"id":7,"categoryId":3,"displayName":"Card","value":"Aasssar"},{"id":8,"categoryId":3,"displayName":"Driving","value":"li"}],"name":"Proof","value":"Proof"}]}

var productType = (obj.master.find(o => o.value === "productType") || {abc: []}).abc;
var proofArr = (obj.master.find(o => o.value === "Proof") || {abc: []}).abc;

console.log( productType );
console.log( proofArr );

答案 1 :(得分:2)

代码中的问题:

  • 状况不佳(完全缺少计数器i)
  • 尝试访问obj [i] .master而不是obj.master
  • 加载校样数组时状况不佳

var obj = {
  "master": [{
      "id": 1,
      "abc": [{
          "id": 1,
          "categoryId": 1,
          "displayName": "Prepaid",
          "value": "PREPAID"
        },
        {
          "id": 2,
          "categoryId": 1,
          "displayName": "Prepaid CYN",
          "value": "PREPAID_CYN"
        }
      ],
      "name": "Product Type",
      "value": "productType"
    },
    {
      "id": 2,
      "abc": [{
        "id": 6,
        "categoryId": 2,
        "displayName": "Mobile",
        "value": "Mobile"
      }],
      "name": "Criteria",
      "value": "criteria"
    },
    {
      "id": 3,
      "abc": [{
          "id": 7,
          "categoryId": 3,
          "displayName": "Card",
          "value": "Aasssar"
        },
        {
          "id": 8,
          "categoryId": 3,
          "displayName": "Driving",
          "value": "li"
        }
      ],
      "name": "Proof",
      "value": "Proof"
    }
  ]
}

let proofArr = [],
  productType = [];

for (var i = 0; i < obj.master.length; i++) {
  if (obj.master[i].value === 'productType') {
    productType = obj.master[i].abc;
  }
  if (obj.master[i].value === 'Proof') {
    proofArr = obj.master[i].abc;
  }
}

console.log(productType)
console.log(proofArr)

答案 2 :(得分:0)

您可以通过value过滤所需的数组,并将找到的所有数组简化为单个数组。

function getByType(type) {
    return obj.master.reduce(
        (r, { value, abc }) => r.concat(value === type ? abc : []),
        []
    );
}

var obj = { master: [{ id: 1, abc: [{ id: 1, categoryId: 1, displayName: "Prepaid", value: "PREPAID" }, { id: 2, categoryId: 1, displayName: "Prepaid CYN", value: "PREPAID_CYN" }], name: "Product Type", value: "productType" }, { id: 2, abc: [{ id: 6, categoryId: 2, displayName: "Mobile", value: "Mobile" }], name: "Criteria", value: "criteria" }, { id: 3, abc: [{ id: 7, categoryId: 3, displayName: "Card", value: "Aasssar" }, { id: 8, categoryId: 3, displayName: "Driving", value: "li" }], name: "Proof", value: "Proof" }] };

console.log(getByType('productType'));
console.log(getByType('Proof'));
.as-console-wrapper { max-height: 100% !important; top: 0; }