Pushing to array

I need to loop through array and each array in array that has extra values, push them to their parent array as separate item. I hope this makes sense..

This is the structure of my initial array:

{type:
    [ 0:
        value: "tomato"
    ],
    [ 1:
        {value: "apple",
        [ extras:
           [ 0: { value: "green" } ],
           [ 1: { value: "red" } ]
        ]
    ],
    [ 2:
        value: "pineapple"
    ]
}

What the result would have to look like:

[type:
    [ 0:
        tomato
    ],
    [ 1:
        apple,
        green,
        red
    ],
    [ 2:
        pineapple
    ]
]

What I've tried and failed: (I also commented the error I get on right line)

var response = /* json of first codeblock in question is response from ajax */;

var items = JSON.parse( response );

var type = Object.keys( items )[0];

var myArray = [] 
var count = items[type].lenght;

//Loop through main items in "type"
for( i = 0; i < count; i++ ) {

    var value = items[type][i][value]; 
    myArray[type][i] = [value];  //Uncaught TypeError: Cannot set property '0' of undefined 

    if( items[type][i][extras] ) {

        var extracount = items[type][i][extras].lenght;

        //Loop through extras
        for( k = 0; k < extracount; k++ ) {

            var extra = items[type][i][extras][k][value];
            myArray[type][i].push( extra );
        }
    }     
}

My main problem that I don't understand and that seems to be the problem in my example as well:

If I declare an empty array, how do I:

  • push an item to that array also declaring a new array around that item?
  • push another item to that array that was made around the first item?

Answers


This is what I believe you want. The following code may be incorrect, because I'm approximating what I believe your items object contains.

var items = {
    type: [
        {
            value: "tomato"
        },
        {
            value: "apple",
            extras: [
                {
                    value: "green"
                }, {
                    value: "red"
                }
            ]
        },
        {
            value: "pineapple"
        }
    ]
};
var myArray = {
    type: []
};


var count = items['type'].length;

//Loop through main items in "type"
for (i = 0; i < count; i++) {

    var subarray = [];
    subarray.push(items['type'][i]['value']);

    if (items['type'][i]['extras']) {
        var extracount = items['type'][i]['extras'].length;
        //Loop through extras
        for (k = 0; k < extracount; k++) {

            var extra = items['type'][i]['extras'][k]['value'];
            subarray.push(extra);
        }
    }
    myArray['type'].push(subarray);
}

Some notes:

You will definitely need to learn the difference between an array and an object in javascript. There are plenty of resources online for this.

When retrieving/manipulating a property prop from an object obj (i.e. for a key-value pair), you will need to use obj.prop or obj['prop']. Note the use of a string in the latter example.

For an array arr, you should use arr.push(value) to push a new value onto the array.


Your problem is here:

 var value = items[type][i][value]; 

you should change it to

 var value = items[type][i].value; 

Need Your Help

Center text vertically and horizontally without absolute positioning

html css html5 css3 center

First of all, it's different from others questions because I can't use position: absolute; as I used usually because of my jQuery plugin (I don't use this plugin in the example, but I have to do po...

How to plot Subset data without the unselected data in R

r plot subset

I want to plot a graph of this (see below), but after subsetting, R is plotting all variables, but only the data of the selected ones.