I have a logic problem here. Let's see if you can help, because I'm burning neurons here and I have not found a starting point.
I have an array of objects and I need to organize them in a way that the previous item can not be the same as 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, will show 3 consecutive blocks of type 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 be clogged with items like text
or just with them, not being able to merge right, but that's 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 ever been in this situation? All help will be welcome :)
PS: Repeat in the sense that the next item can be a type image
or video
, but can not have 2 text
followed (save situation where you can not organize because only text or many texts) / p>