0

i have succesfully Fetched the data i wanted but i am unable to print the titles, i need to check the titles of the Data i am getting. what should i do to print those titles or save them in an array. thanks a lot.

enter image description here

console.log(data);

{docs: Array(13)}
docs: Array(13)
0: {id: "5decc1d526e8707d4473c3ef", title: "11Records.csv"}
1: {id: "5decc1ff26e8707d4473c3fe", title: "thisIsNewReport.csv"}
2: {id: "5decc35d26e8707d4473c40d", title: "a.csv"}
3: {id: "5decc3bd26e8707d4473c41c", title: "a.csv"}
4: {id: "5decc3d826e8707d4473c42b", title: "a.csv"}
5: {id: "5decc4d826e8707d4473c43a", title: "thisIsNewReport.csv"}
6: {id: "5decc5d826e8707d4473c449", title: "thisIsNewReport.csv"}
7: {id: "5decc63626e8707d4473c458", title: "thisIsNewReport.csv"}
8: {id: "5decc66f26e8707d4473c467", title: "thisIsNewReport.csv"}
9: {id: "5deccab726e8707d4473c476", title: "thisIsNewReport.csv"}
10: {id: "5deccb5926e8707d4473c485", title: "thisIsNewReport.csv"}
11: {id: "5deccb8d26e8707d4473c494", title: "thisIsNewReport.csv"}
12: {id: "5deccba926e8707d4473c4a3", title: "thisIsNewReport.csv"}
length: 13
__proto__: Array(0)
__proto__: Object

3 Answers3

0

this is very simple, just do this :

let fileName = "test.cv";
data.docs.forEach(item => {       
    if (fileName == item.title) {
        console.log("this is the file your are looking for ", item);
    }
})
  • yes it works , and how can i save it in an array and then check if the a text file name belongs to this array of titles or not? i am new to JS and react :S – user3296651 Dec 08 '19 at 10:29
0

You can do something like

const result = data.docs.map(doc => doc.title);

This contains only the titles, then result.includes("mytext") to check if your text exists in the array.

Sam
  • 572
  • 3
  • 11
0

In order to save titles in new array you should use "map" array function:

const result = data.docs.map(({ title }) => title)

To print titles use forEach function which doesn't create new array just iterate over this one:

data.docs.forEach(({ title }) => {
  // here you can do the title checking
  console.log(title)
})
viciousP
  • 510
  • 3
  • 14