3

can you help me please with iteration the array , Here is my code

https://jsfiddle.net/Ar2zee/9g91ouq6/1/

var projects = {
  "projects" : [[
    "Title : Portfolio",
    "Dates : 2017",
    "Description : Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt voluptatem ex eius sapiente eum quod nostrum esse dolorem sequi deleniti!",
     ["images/1.jpg","images/2.jpg","images/3.jpg","images/4.jpg"]
  ],
  [
    "Title : Social Network",
    "Dates : 2019",
    "Description : Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt voluptatem ex eius sapiente eum quod nostrum esse dolorem sequi deleniti!",
    ["images/1.jpg" , "images/2.jpg" , "images/3.jpg" , "images/4.jpg"]
  ]]
};

for (var i = 0 ; i < projects.projects[3].length; i++) {
  var formattedImage = HTMLprojectImage.replace("%data%",projects.projects[3][i])
}

How to iterate thru images ? Thank you so Much !

JV Lobo
  • 5,526
  • 8
  • 34
  • 55
Ar2zee
  • 410
  • 1
  • 4
  • 12
  • @charlietfl - Could you please re-open this at least for a moment? This is a real "XY" question. One problem is that the data format is pretty messed up and awkward to work with. I had just finished writing an answer suggesting an improvement to the data structure, but can't post it now. Yes, the OP could write code to deal with the data as it is, but I think it would be beneficial if they could see an alternative way to format the data. I saved my answer in a text file in anticipation of your re-opening the question. :-) Thanks! – Michael Geary Jul 13 '17 at 01:02
  • @MichaelGeary done. I really marked as duplicate to a pretty good tutorial like answer a lot because question was so vague but agree about messed up structure – charlietfl Jul 13 '17 at 01:03
  • I reopen the question , SIr – Ar2zee Jul 13 '17 at 01:12
  • Thank you my friends! Answer posted. – Michael Geary Jul 13 '17 at 01:15
  • @charlietfl It turns out my idea of a better JSON format isn't helpful, since OP is required to use the format given. So feel free to re-close if that makes sense. At least someone running across the question in the future may see my suggestion and look at using a better data format, so I won't feel like my time was wasted. :-) – Michael Geary Jul 13 '17 at 01:34

2 Answers2

2
for (project of projects['projects']) {
  for (image of project[3]) {
    console.log('This is image: ' + image);
  }   
}

This will iterate through the images. [JS Bin]

Daniel
  • 652
  • 5
  • 8
  • The JSBin seems to be something unrelated, is that the right link? Also you'd better use `var` or `let` on your `project` and `image` variables. And just a matter of taste, but it's a little cleaner to use `projects.projects` instead of `projects['projects']` - either will do exactly the same thing. – Michael Geary Jul 13 '17 at 01:26
  • Actually can ou help me please again beacause your code iterated thru all images but I need separate iteration from each of them cause it's supposed to be 8 different images 4 for each of them. – Ar2zee Jul 13 '17 at 01:35
0

The format of your projects data is pretty awkward. Are you required to use this data format, or do you have the possibility of changing the data format to make it easier to work with in JavaScript?

A more practical JSON object for your data might look like this:

{
    "projects": [
        {
            "Title": "Portfolio",
            "Dates": "2017",
            "Description": "Lorem ipsum dolor sit amet...",
            "Images": [
                "images/1.jpg",
                "images/2.jpg",
                "images/3.jpg",
                "images/4.jpg"
            ]
        },
        {
            "Title": "Social Network",
            "Dates": "2019",
            "Description": "Lorem ipsum dolor sit amet...",
            "Images": [
                "images/1.jpg",
                "images/2.jpg",
                "images/3.jpg",
                "images/4.jpg"
            ]
        }
    ]
}

Let me know if you are allowed to change the data format and I can offer some more suggestions. And questions: the name of the Dates value suggests that it might want to have multiple dates, not just one, so maybe it should be an array?

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • Actually No , I need to have all data here as an array , with an object it will be a way easier, and it's why I'm messed up with iteration – Ar2zee Jul 13 '17 at 01:15
  • @ArturOganesyan Ah well, those are the breaks! Looks like Daniel's code should do the trick for you then. – Michael Geary Jul 13 '17 at 01:16
  • It's very weird but when I use Daniel's code I have my images twice instead to have it just once but console show me correct output how it supposed to be with all images once for each array – Ar2zee Jul 13 '17 at 01:18