-4

I have an array inside an object as below:

[
{ "id": 244, "title": "myBook.pdf", "author": "Kevin"},
{ "id": 370, "title": "Bookpress.pdf", "author": "Justin"},
{ "id": 433, "title": "Uptown.pdf", "author": "David"},
]

How can I get all the data of all title data? Data required is: myBook.pdf, Bookpress.pdf, Uptown.pdf

prasanth
  • 22,145
  • 4
  • 29
  • 53
CloudSeph
  • 863
  • 4
  • 15
  • 36
  • Unless you tried, we are not gonna help you. – Rahul Aug 16 '17 at 04:54
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Rahul Aug 16 '17 at 04:57
  • Possible duplicate of [Get single value from JSON object](https://stackoverflow.com/questions/37570568/get-single-value-from-json-object) – Rahul Aug 16 '17 at 04:57

3 Answers3

0

If you are using lodash, you can simply do this

var data = [
  { "id": 244, "title": "myBook.pdf", "author": "Kevin"},
  { "id": 370, "title": "Bookpress.pdf", "author": "Justin"},
  { "id": 433, "title": "Uptown.pdf", "author": "David"},
]

var result = _(data).map("title").value();

Gives you

result
(3) ["myBook.pdf", "Bookpress.pdf", "Uptown.pdf"]

But since this sounds like is an assignment question, you should try to do this by yourself without libraries.

Daryl Teo
  • 5,394
  • 1
  • 31
  • 37
0

Simply use Array#map function .use join(',') function for applying comma

var arr = [
{ "id": 244, "title": "myBook.pdf", "author": "Kevin"},
{ "id": 370, "title": "Bookpress.pdf", "author": "Justin"},
{ "id": 433, "title": "Uptown.pdf", "author": "David"},
]

console.log(arr.map(a=> a.title))
console.log(arr.map(a=> a.title).join(','))
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

Use a for loop to loop through the array and use javascript object notation to fetch the desired object property and append that into the html or use in whatever way that you like.

var arr =[
{ "id": 244, "title": "myBook.pdf", "author": "Kevin"},
{ "id": 370, "title": "Bookpress.pdf", "author": "Justin"},
{ "id": 433, "title": "Uptown.pdf", "author": "David"},
]

for (var i =0;i < arr.length;i++){

var el = document.getElementById("demo");
el.innerHTML += arr[i].title
}
<div id="demo">
</div>
shikhar
  • 174
  • 1
  • 11