Javascript Array / Object problem

If I use the following code, I will get as result a JSON file with:

[{id: "261"}, {value: "BAU15961"}, {ref: 0}], [{id: "268"}, {value: "BAU10942"}, {ref: 0}],<..>]

Open in new window


But I need:
[{id: "261", value: "BAU15961", ref: 0}, {id: "268", value: "BAU10942", ref: 0},<..>]

Open in new window


What I have done wrong.

The code:
function checkMultiArray(myArray,myString){
    var result;

    for( var i = 0, len = myArray.length; i < len; i++ ) {
        if( myArray[i][1].value === myString ) {
            return true;
            break;
        }
    }
    return false;
}

function treeDataPrepare(){

    var datanarray = [];
    $$("myBoard").eachList(function(list,status){
        if (list) {
            if(status=="Inbox"||status=="Fehler"||status=="Sonder"||status=="Prüfen"){
                
                list.data.each(function(obj){
                    var mainarray = [];
                    var ident = obj.name;

                    
                    if(checkMultiArray(datanarray,ident)===false){

                            //Nicht vorhanden, also fügen wir es hinzu

                            mainarray.push({"id":obj.id});
                            mainarray.push({"value":obj.name});
                            mainarray.push({"ref":0});

                            console.log("mainarray loop: "+obj.name);


                       
                        datanarray.push(mainarray);
                    }


                });


            }
        }

    });

    var myJsonString = JSON.stringify(datanarray);

    console.log(myJsonString);

    return myJsonString;

}

Open in new window

Ingo FoersterProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Your problem is on lines 29-31 you are adding each of the objects as an individual array item instead of adding one object.
You need to change this
mainarray.push({"id":obj.id});
mainarray.push({"value":obj.name});
mainarray.push({"ref":0});

Open in new window

To this
mainarray.push({
  id:obj.id,
  value:obj.name,
  ref:0
})

Open in new window

Now you are pushing a single object with all 3 properties into the array.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ingo FoersterProgrammerAuthor Commented:
Hello Julian, thank you for your quick response. But with your tip I have

[[{"id":"261","value":"BAU15961","ref":0}],[{"id":"268","value":"BAU10942","ref":0}]

Open in new window

finally not
[{id: "261", value: "BAU15961", ref: 0}, {id: "268", value: "BAU10942", ref: 0},<..>]

Open in new window

0
Julian HansenCommented:
Then get rid of mainarray altogether and replace with datanarray

The way you have it you are putting the objects into an array and then adding that array to your main array. If you look at the desired JSON you want an array with objects as elements (not arrays)

Just do this

function treeDataPrepare()
{
  var datanarray = [];
  $$("myBoard").eachList(function(list,status){
    if (list) {
      if(status=="Inbox"||status=="Fehler"||status=="Sonder"||status=="Prüfen"){
        
        list.data.each(function(obj){
          var ident = obj.name;

          if(checkMultiArray(datanarray,ident)===false){

              //Nicht vorhanden, also fügen wir es hinzu

              datanarray.push({"id":obj.id});
              datanarray.push({"value":obj.name});
              datanarray.push({"ref":0});

              console.log("mainarray loop: "+obj.name);
          }

        });
      }
    }
  });

  var myJsonString = JSON.stringify(datanarray);

  console.log(myJsonString);

  return myJsonString;
}

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

it_saigeDeveloperCommented:
*NO POINTS*

Just a small correction to Julian's last post -
function treeDataPrepare()
{
  var datanarray = [];
  $$("myBoard").eachList(function(list,status){
    if (list) {
      if(status=="Inbox"||status=="Fehler"||status=="Sonder"||status=="Prüfen"){
        
        list.data.each(function(obj){
          var ident = obj.name;

          if(checkMultiArray(datanarray,ident)===false){

              //Nicht vorhanden, also fügen wir es hinzu

              datanarray.push({id:obj.id, value:obj.name, ref:0});

              console.log("mainarray loop: "+obj.name);
          }

        });
      }
    }
  });

  var myJsonString = JSON.stringify(datanarray);

  console.log(myJsonString);

Open in new window


-saige-
0
Ingo FoersterProgrammerAuthor Commented:
Great Help. Many thanks!
0
Julian HansenCommented:
@It_saige - thank you - I copied code from OP instead of my previous answer - and did not make my suggested modifications. Thanks for picking up.

@Ingo - you are welcome.
0
it_saigeDeveloperCommented:
@Julian,

No worries.  You would have done the same.

-saige-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.