-4

Hello Im relative new into JQuery... more used to PHP Its just really simple I think but Im too stupid and if im honest JS cracks my mind....(in OOP Ive to learn much too...)

So I already found out that there arent arrays like in PHP.

Ok lets start.

My HTML Looks like this:

<div id="content">
    <ul id="ul_1"></ul>
    <ul id="ul_2"></ul>
    <ul id="ul_3"></ul>
</div>

And now I want to generate this out of that anyway

controller = {
    "#ul_1": 1,
    "#ul_2": 1,
    "#ul_3": 1
};

So to be more precise, at moment its hard coded like the code example above this line. But I want that it generates automatically out of the HTML or better DOM.

I already tried .map, .find and .each... but im too disabled.

I always prefered it make it at once, so I mean no iteration with .each if that would be possible. I think maybe its possible to create a new object out of the $.('#content') object but in the style I need it.

Thank very much you Rycochet

heres the solution for my case

var controller = {};
$("#content > ul").each(function (i, el) {
    controller['#' + $(el).attr('id')] = 1;
});

Is there another solution, too? Like with .map or is that the best, fastest and easiest way to do it?

Tuppabox
  • 3
  • 4

1 Answers1

0

Totally guessing here - but if your data is something like this -

<div id="content">
    <ul id="ul_1">
        <li>isn</li>
        <li>staff</li>
        <li>product</li>
    </ul>
</div>

Then you can make such an object by -

var controller = {};
$("#ul_1>li").each(function(i, el) {
    controller[$(el).text()] = 1;
});

As the comments above say - you've not put enough details for more than a guess though.

Rycochet
  • 2,860
  • 1
  • 22
  • 39