1
I have a logic problem here. Let’s see if you can help, because I’m burning neurons here and I haven’t found a starting point.
I have an array of objects and need to organize it in a way that the previous item cannot be equal to the next item. Something like this
var result = [
{ type: 'image', name: 'foo image', src: 'http://www.something.com/path/to/image.jpg', text: "lorem ipsum sit amet" },
{ type: 'video', name: 'foo video', src: 'http://www.something.com/path/to/video.mp4', text: "lorem ipsum sit amet" },
{ type: 'text', name: 'foo text', src: '', text: "lorem ipsum sit amet" },
{ type: 'text', name: 'foo text', src: '', text: "lorem ipsum sit amet" },
{ type: 'text', name: 'foo text', src: '', text: "lorem ipsum sit amet" },
{ type: 'video', name: 'foo video', src: 'http://www.something.com/path/to/video.mp4', text: "lorem ipsum sit amet" },
{ type: 'image', name: 'foo image', src: 'http://www.something.com/path/to/image.jpg', text: "lorem ipsum sit amet" },
{ type: 'text', name: 'foo text', src: '', text: "lorem ipsum sit amet" },
{ type: 'video', name: 'foo video', src: 'http://www.something.com/path/to/video.mp4', text: "lorem ipsum sit amet" }
];
The way it is, when it is displayed on the screen, it will show 3 blocks in a row text
in the array interaction. It would be necessary to make an interaction, to balance the items to the maximum, not to leave two blocks followed by text. Of course the array can come clogged with items of the type text
or only with them, not being able to interim duty, but this is not the case now.
From the above item, the result would have to look something like this:
var result = [
{ type: 'image', name: 'foo image', src: 'http://www.something.com/path/to/image.jpg', text: "lorem ipsum sit amet" },
{ type: 'video', name: 'foo video', src: 'http://www.something.com/path/to/video.mp4', text: "lorem ipsum sit amet" },
{ type: 'text', name: 'foo text', src: '', text: "lorem ipsum sit amet" },
{ type: 'video', name: 'foo video', src: 'http://www.something.com/path/to/video.mp4', text: "lorem ipsum sit amet" },
{ type: 'text', name: 'foo text', src: '', text: "lorem ipsum sit amet" },
{ type: 'image', name: 'foo image', src: 'http://www.something.com/path/to/image.jpg', text: "lorem ipsum sit amet" },
{ type: 'text', name: 'foo text', src: '', text: "lorem ipsum sit amet" },
{ type: 'video', name: 'foo video', src: 'http://www.something.com/path/to/video.mp4', text: "lorem ipsum sit amet" }
{ type: 'text', name: 'foo text', src: '', text: "lorem ipsum sit amet" }
];
Has anyone been through this? Help will be welcome :)
PS: repeat in the sense that the next item may be a type image
or video
, but cannot have 2 text
followed (unless there is no way to organize by having only text or many texts)
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack