Add existing variable to array in javascript -


After

I have found this HTML

  & lt; div class = "calListChip" id = "label bDd1aDFjNnQ2aHFxOTN2cGQyM2JhaXA2cmtAZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ" title = "Testkalender1" & gt; & Lt; div class = "calListChip" id = "label OWFmbmdwbWprbTRxMmFrOTNycGlicmM2bjBAZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ" title = "Testkalender2" & gt;  

And I've got this code, as the name of the variable with the title of an HTML div and then I pass the label of the html object as the parameter for the variable

var element = document.body.getElementsByClassName ('Callistist'); Var view1 = []; // found for every element (var i = 0; i & lt; elements.length; i ++) {// Variable names cant inlude spaces var title = elements [i] .title; Title = title. Location (/ + / g, ""); // Create a Variable Window [Title] = Element [I] .ID; // if 'tests' are present in the title, if you add the variable to an array (elements [i] .title.toLowerCase () indexOf ( "test") & gt ;. = 0) {view1.push (window [Title ]); }};

But the view1 array dosent gets variable references but instead of the title name is a string, which I can not use later.

This is the result I want

  View1: [Testkalender1, Testkalender2]  

This is the result I get

< pre> View1: [ "Testkalender1", "Testkalender2"]

the problem I do not know how many or title / label element of html I need to dynamically variable form And then they are inserted into the right array by any keyword in the title

Any suggestions?

It is not clear how you can create your desired result (for example, from where the data Comes), but I can help explain what is happening with your current code. When you do this:

  window [title] = element [i] .id;  

You are creating a global variable named title and assigning it a string value located in elements [i] .id Are there. Therefore, now you have a global variable with a string.

When you do this later:

  view1.push (window [title])  

You just have to do that string Pushing a copy into the view1 array. If this string is not to do anything with your global variable, then there will be an array of view1 strings.


If you want view1 array Here's an example of its object:

  Scene 1 [0] = {name : 'Test calendars', items: [testkalender1, testkalender2]};  

Again, you have to put objects in the array, not strings.


I should add that I do not know how to construct this global variable:

  window [title] = element [i] .id;  

Is doing it to help your problem in any way. This should not be necessary.


I would be happy to help you get a different result, but by now you have shown a desired array of items, but we are not shown where you get it item property from> name If you can edit your question to show that value, then we can help it with a solution to generate it.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -