1

I am getting response from service like this

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)

I want to transform or get array from response.

expected output

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"
          }
        ]

how I will get data from response ?

4
  • You need to format the data from the service to how you like... Have you tried that ? Commented Aug 28, 2018 at 9:35
  • Have you used Object.entries()? Commented Aug 28, 2018 at 9:37
  • Also obj is not an array, so itterating over it with for loop doesnt make sense.. use obj.master[i] jsfiddle.net/ce01rs8q/2 Commented Aug 28, 2018 at 9:39
  • @Pogrindis we are using two loops , can we optimise this Commented Aug 28, 2018 at 9:42

3 Answers 3

4

Another option you have is to use find() to search for the first match.

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 );

Sign up to request clarification or add additional context in comments.

1 Comment

So pretty, but obligatory IE warning for find :(
2

Problems in your code:

  • bad condition (totally missing the counter i)
  • trying to access obj[i].master instead obj.master
  • bad condition when you are loading the proof array

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)

Comments

0

You could filter the wanted arrays by value and reduce all found arrays to a single array.

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.