-1

This is the results I am getting using console.log(res)

{d: Array(4)}
d: Array(4)
0: {__type: "Claim_Inbox+SubClaims", Claim_detail: 12, Claim_id: 123, …}
1: {__type: "Claim_Inbox+SubClaims", Claim_detail: 12, Claim_id: 124, …}
2: {__type: "Claim_Inbox+SubClaims", Claim_detail: 12, Claim_id: 125, …}
3: {__type: "Claim_Inbox+SubClaims", Claim_detail: 12, Claim_id: 126,…}
length: 4
__proto__: Array(0)
__proto__: Object

Using the following code I am getting the list of keys:values

var counter = 0;
$.each(res.d[counter ], function (key, value) {
   console.log(key + ": " + value);
   counter++;
});

how can I read a specific value like Claim_id ? I can use Switch() but maybe there is another way

Eyal
  • 4,653
  • 9
  • 40
  • 56
  • what do you get with ``res.d.forEach(console.log)``, – r g Feb 06 '19 at 14:44
  • 4
    Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) and [Returning only certain properties from an array of objects in Javascript](https://stackoverflow.com/questions/24440403) and [Get array of property values from array of objects with jquery](https://stackoverflow.com/questions/29472655) – adiga Feb 06 '19 at 14:45
  • Can you post the raw JSON or the source? –  Feb 06 '19 at 14:46
  • you cannot use .forEach here – Eyal Feb 06 '19 at 14:55

1 Answers1

0

First of all, it is not an Object, it is an Array. Second, depending on what you want, you can use forEach to loop over the array and show or assign or do whatever you need with the id OR use map to construct a new array with the selected values.

const array = [
  { __type: "Claim_Inbox+SubClaims", Claim_detail: 12, Claim_id: 123 },
  { __type: "Claim_Inbox+SubClaims", Claim_detail: 12, Claim_id: 124 },
  { __type: "Claim_Inbox+SubClaims", Claim_detail: 12, Claim_id: 125 },
  { __type: "Claim_Inbox+SubClaims", Claim_detail: 12, Claim_id: 126 }
];

array.forEach(item => console.log(item['Claim_id']); );

console.log("--------------------");

// Generate a new array copy
const result = array.map(item => item['Claim_id']);

console.log(result)
Prince Hernandez
  • 3,623
  • 1
  • 10
  • 19
  • ``Array`` is an ``Object`` in JS – r g Feb 06 '19 at 14:48
  • @fard yes, they are, but also are functions.... not for that you can loop over a function.... the base of javascript are objects, but they are not the same type of object ;) – Prince Hernandez Feb 06 '19 at 15:44
  • But you can't say that Arrat is not an object, like you do in your first sentence @PrinceHernandez – r g Feb 07 '19 at 08:07