0

The backend of my webapp, written in node.js interacts with Json file, with a specific format that I thought not so complex but apparently is.

The structure of my json file is as such :

{
    "data": [
      {
        "somefield": "ioremipsum",
        "somedate" : "2018-08-23T11:48:00Z",
        "someotherdate" : "2018-08-23T13:43:00Z",
        "somethingelse":"ioremipsum",
        "files": [
          {
            "specificfieldinarray": "ioremipsum",
            "specificotherfieldinarray": "ioremipsum"
          },
          {
            "specificfieldinarray": "ioremipsum",
            "specificotherfieldinarray": "ioremipsum"
          },
          {
            "specificfieldinarray": "ioremipsum",
            "specificotherfieldinarray": "ioremipsum"
          }
        ]
      }
    ]
  }

I try to make this answer fit a JS object like this :


const file =  require('specificJsonFile.json');

let fileList = file;

And I need to loop through my 'files' array, for further treatments, but unfortunately, my JS object looks like this :


{ data:
   [ { somefield: "ioremipsum",
       somedate : "2018-08-23T11:48:00Z",
       someotherdate : "2018-08-23T13:43:00Z",
       somethingelse:"ioremipsum",
       files: [Array] } ] }

Please forgive me if this is obvious, for I am still a beginner with JS.

Simon Grondin
  • 79
  • 1
  • 9
  • What kind of loop do you want to use? Are you trying to change the items in files array, or read them for some other use? – Cal Irvine Jun 13 '19 at 15:49
  • 1
    Seems okay to me. Since `files` is a nested array inside an object inside an array which _also_ contains objects, the console log is abbreviated. You should be able to get to your array of files by doing `fileList.data[0].files.` – Ryan Dabler Jun 13 '19 at 15:51
  • I just want to read the items for further use. Currently I try to use a for(var file in files){}. – Simon Grondin Jun 13 '19 at 15:54
  • @RyanDabler , actually, I can't. I loop through data like this : ```for( var x in fileList.data){for(var file in x.files){}}``` . And none of the treatment in the second loop is reached,also when I try to fit the 'files' array in an object, the variable logs 'undefined' in the console. – Simon Grondin Jun 13 '19 at 15:55
  • That's because the `for` `in` structure gives you a numerical index, not the element itself. You can use `for` `of` if you want the element or use `fileList.data[x]` in your second loop. – Lifz Jun 13 '19 at 16:00
  • Well, false alarm, I just was confused with basics of loops through js arrays. Thanks alot nevertheless. – Simon Grondin Jun 13 '19 at 16:07
  • @Lifz yes, it was absolutely that. Thank you. – Simon Grondin Jun 13 '19 at 16:08
  • @SimonGrondin no problem :) – Lifz Jun 13 '19 at 16:25

4 Answers4

1

That's only how console.log logs deep objects. To get a deeper output, you can use util.inspect

const util = require('util');
console.log(util.inspect(yourObject, {showHidden: false, depth: null}));

To loop each data's files, simply loop data, then its files

yourObject.data.forEach(d => {
    d.files.forEach(file => console.log(file));
});
baao
  • 71,625
  • 17
  • 143
  • 203
1

It looks like there is nothing wrong there and the console is abbreviating the log.

Try accessing the files list with the following code:

const filesList = file.data[0].files

and then

console.log(filesList) to check that it's eventually working.

Hope it helps!

Alvaro
  • 1,853
  • 18
  • 24
-1
let fileList = file.data[0].files;

This will create an array of only your files array.

You can console.log(fileList)

Or whatever you like with the data.

Based on your comment, try the of keyword instead of in keyword to get the behaviour you expected.

for (let file of fileList){
    console.log(file);
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

Cal Irvine
  • 1,104
  • 8
  • 17
  • Actually, thank you. I misunderstood the fact the var you define is not the item, but the index when looping through arrays. I am still very easily confused by this. I'll accept your answer since you were the first one to do so. Cheers. – Simon Grondin Jun 13 '19 at 16:05
  • @SimonGrondin __never__ use `for in` on an array!!! This answer is not correct, you should unmark it. https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea – baao Jun 13 '19 at 16:10
  • @bambam as per mdn: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Array_iteration_and_for...in This is only true in an array where the index order is important. Anyway, I updated the answer to use for...of instead of for...in as it is the behaviour closer to what SimonGrondin was asking for. – Cal Irvine Jun 13 '19 at 16:17
-1

You can use for in

for (item in fileList.data) {
    for (file in fileList.data[item].files) {
         let data = fileList.data[item].files[file];

         // process the data         

    }
}
yovie
  • 81
  • 2