2

i'm using the tree component in my angular projet, i'm trying to loop a json object to populate the tree heres my code:

 var myData=[{"agence":"CTM","secteur":"Safi","serie":"CZC1448YZN"},{"agence":"CTM","secteur":"Safi","serie":"2UA13817KT"},{"agence":"CTM","secteur":"Safi","serie":"2UA13817KT"},{"agence":"CTM","secteur":"Essaouira","serie":"CZC1221B85"},{"agence":"CTM","secteur":"Essaouira","serie":"CZC1221B85"},{"agence":"Gare Routiere Municipale","secteur":"Essaouira","serie":"CZC145YL3"},{"agence":"Gare Routiere Avenue des FAR CTM","secteur":"Casablanca","serie":"2AU2290Y48"},{"agence":"Gare Routiere Avenue des FAR CTM","secteur":"Casablanca","serie":"2AUD14404X"}];
    for(var i=0; i < myData.length; i++) {
       // alert('roro '+myData[i].secteur);
        treedata_avm = [{
            label: myData[i].secteur,
            children: [{
                label: myData[i].agence,
                children: [myData[i].serie]
            }]
        }];
    }

My problem is, i got only the last line in the json object which is {"agence":"Gare Routiere Avenue des FAR CTM","secteur":"Casablanca","serie":"2AUD14404X"}

how i can get all the data displayed in the tree???

1
  • use angular.forEach(jsonObject, function(singleObject,key){ //logic }) Commented Dec 30, 2016 at 10:12

4 Answers 4

1

You are replacing the previous value in treedata-avm instead of adding the new one into it. What you can do is, define an empty array treedata_avm and push each object in myData into it by iterating which you are already doing.

var myData=[{"agence":"CTM","secteur":"Safi","serie":"CZC1448YZN"},{"agence":"CTM","secteur":"Safi","serie":"2UA13817KT"},{"agence":"CTM","secteur":"Safi","serie":"2UA13817KT"},{"agence":"CTM","secteur":"Essaouira","serie":"CZC1221B85"},{"agence":"CTM","secteur":"Essaouira","serie":"CZC1221B85"},{"agence":"Gare Routiere Municipale","secteur":"Essaouira","serie":"CZC145YL3"},{"agence":"Gare Routiere Avenue des FAR CTM","secteur":"Casablanca","serie":"2AU2290Y48"},{"agence":"Gare Routiere Avenue des FAR CTM","secteur":"Casablanca","serie":"2AUD14404X"}]; 

var treedata_avm = [];

for(var i=0; i < myData.length; i++) {
   // alert('roro '+myData[i].secteur);
    treedata_avm.push({
        label: myData[i].secteur,
        children: [{
            label: myData[i].agence,
            children: [myData[i].serie]
        }]
    });
}
console.log(treedata_avm);
Sign up to request clarification or add additional context in comments.

6 Comments

It's working fine. Copy and paste the same code in browser console and check please.
please..another question...i got a duplicate data in my tree..is there a way to group by secteur in the json object
You can check for the duplicates in the loop avoid pushing it into the array or for a simpler solution , use lodash library which has a many methods to these kind of operations lodash.com/docs/4.17.2#uniq
no..i will lose that like that..i want to group by secteur in my json object..this is exactly what i want help please..
Then you can use _.groupBy method
|
0
 var myData=[{"agence":"CTM","secteur":"Safi","serie":"CZC1448YZN"},{"agence":"CTM","secteur":"Safi","serie":"2UA13817KT"},{"agence":"CTM","secteur":"Safi","serie":"2UA13817KT"},{"agence":"CTM","secteur":"Essaouira","serie":"CZC1221B85"},{"agence":"CTM","secteur":"Essaouira","serie":"CZC1221B85"},{"agence":"Gare Routiere Municipale","secteur":"Essaouira","serie":"CZC145YL3"},{"agence":"Gare Routiere Avenue des FAR CTM","secteur":"Casablanca","serie":"2AU2290Y48"},{"agence":"Gare Routiere Avenue des FAR CTM","secteur":"Casablanca","serie":"2AUD14404X"}];

var treedata_avm = [];
for(var i=0; i < myData.length; i++) {
   // alert('roro '+myData[i].secteur);
    treedata_avm.push({
        label: myData[i].secteur,
        children: [{
            label: myData[i].agence,
            children: [myData[i].serie]
        })
    }];
}

you should use array function 'push' to push the object to the new array; but not use the '=';

Comments

0

Try this:

var treedata_avm = [];
for (var i = 0; i < myData.length; i++) {
    treedata_avm.push({
        label: myData[i].secteur,
        children: [{
            label: myData[i].agence,
            children: [myData[i].serie]
        }]
    });
}

Using .push(). will work for you.

Comments

0

You can also use the map function, which is functional and a little less error prone https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array/map

var treedata_avm = myData.map(function(value) {
    return {
        label: myData[i].secteur,
        children: [{
            label: myData[i].agence,
            children: [myData[i].serie]
        }]
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.