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 !

5
  • @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! Commented Jul 13, 2017 at 1: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 Commented Jul 13, 2017 at 1:03
  • I reopen the question , SIr Commented Jul 13, 2017 at 1:12
  • Thank you my friends! Answer posted. Commented Jul 13, 2017 at 1: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. :-) Commented Jul 13, 2017 at 1:34

2 Answers 2

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]

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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?

3 Comments

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
@ArturOganesyan Ah well, those are the breaks! Looks like Daniel's code should do the trick for you then.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.