-2

I have a simple json with an array of objects, I managed to extract the record but it comes up with square brackets, and I want just the object.

var res = {
  "totalSize": 3,
  "done": true,
  "records": [{
    "Id": "1",
    "CaseNumber": "1",
    "Subject": "test1"
  }, {
    "Id": "2",
    "CaseNumber": "2",
    "Subject": "test2"
  }, {
    "Id": "3",
    "CaseNumber": "3",
    "Subject": "test3"
  }]
}

var extractedRecord = res.records.slice(0, 1);

Desired outcome:

  {
    "Id": "1",
    "CaseNumber": "1",
    "Subject": "test1"
  }

Actual outcome:

[
  {
    "Id": "1",
    "CaseNumber": "1",
    "Subject": "test1"
  }
]

See this jsfiddle.

adiga
  • 34,372
  • 9
  • 61
  • 83
Json
  • 655
  • 10
  • 27
  • Read about [`Array.slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice). And about arrays, [in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) and in general. – axiac Dec 30 '19 at 12:37
  • Just use `res.records[0]` – adiga Dec 30 '19 at 12:48
  • Why was my question downvoted? At least provide some pointers for me to learn for future cases – Json Dec 30 '19 at 13:19

3 Answers3

1

Just do res.records.slice(0, 1)[0]

var res = {"totalSize":3,"done":true,"records":[{"Id":"1","CaseNumber":"1","Subject":"test1"},{"Id":"2","CaseNumber":"2","Subject":"test2"},{"Id":"3","CaseNumber":"3","Subject":"test3"}]}

var extractedRecord = res.records.slice(0, 1)[0];
console.log(extractedRecord);

You can also use spread syntax:

var res = {"totalSize":3,"done":true,"records":[{"Id":"1","CaseNumber":"1","Subject":"test1"},{"Id":"2","CaseNumber":"2","Subject":"test2"},{"Id":"3","CaseNumber":"3","Subject":"test3"}]}

var extractedRecord = res.records.slice(0, 1);
console.log(...extractedRecord);
Ankita Kuchhadiya
  • 1,255
  • 6
  • 16
1

Arrray.prototype.slice(), as its documention states, returns a new array from an original one.

From MDN (my emphasis):

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.

Knowing this, it's no surprise you're getting an array returned (i.e. your object encased with [ and ]). Therefore you just need to extract the only item in it:

var extractedRecord = res.records.slice(0, 1)[0];
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • 1
    I accepted this answer and not the others because @Utkanos provided explanations and not just the code. Cheers. – Json Dec 30 '19 at 13:26
0

Try this code.It will works for you.

var res = {"totalSize":3,"done":true,"records":[{"Id":"1","CaseNumber":"1","Subject":"test1"},{"Id":"2","CaseNumber":"2","Subject":"test2"},{"Id":"3","CaseNumber":"3","Subject":"test3"}]}

const data = res.records[0]
console.log(data);
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
  • Please provide some explanation, otherwise the OP has no way of knowing what they did wrong. – Mitya Dec 30 '19 at 12:46
  • They did not wrong anything,but this is other way to achieve the answer of question and also getting without slice method. – Pushprajsinh Chudasama Dec 30 '19 at 12:50
  • This is the only right answer, as in, there is no point of using `slice()` when all you want to do is to access an array element. – Michał Sadowski Dec 30 '19 at 13:06
  • @MichałSadowski yes and no. You are right that this answer shows the shortest route to achieving the desired output. But insofar as the OP expressed confusion about how `slice()` works, it is worth explaining that erroenous thinking. – Mitya Dec 30 '19 at 13:11
  • @Utkanos true, but it also, I believe, is important to point out how accessing elements of an array should be done. – Michał Sadowski Dec 30 '19 at 13:14