1

I have this code:

<?php 
if(mysqli_num_rows($result2) >= 2){
foreach($label_name as $meal_option_id => $names_of_labels)
{
    echo '<button price='.$protein_prices[$meal_option_id].' label_option= '.$meal_option_id.'  data-label-option= '.$meal_option_id.' class="view1 white cbtn1 open_sansbold option protein-option">' .$names_of_labels. ' <span id="price-difference-for-'.$meal_option_id.'"></span></button>';
}}?>

Javascript:

$(function() {
    var meal_qty = new Array(); 
    var label_options = new Array(); 
    var qty_options = new Array();

    $(".protein-option").click(function() {
        var meal_label_qty = $(this).data("label-option");
        // CREATE THE ASSOCIATIVE LABEL
        meal_qty[meal_label_qty] = [];
        // CREATE THE OBJECT WITH VALUES
        var item = {
            mon: $("#qty1").val(),
            tues: $("#qty2").val(),
            wed: $("#qty3").val(),
            thur: $("#qty4").val(),
            fri: $("#qty5").val()
        }
        // ADD TO ARRAY
        meal_qty[meal_label_qty] = item;

        console.log(meal_qty);
    });
});

Now item does contain the right values and so does meal_label_qty but my console log is:

(1003) [undefined × 1002, Object]

I wanted this output:

[meal_label_qty]
{
                mon: $("#qty1").val(),
                tues: $("#qty2").val(),
                wed: $("#qty3").val(),
                thur: $("#qty4").val(),
                fri: $("#qty5").val()  //WHICH IS ITEM
}

I realized my code goes the right meal_label_qty and item but starts writing Object after this code line meal_qty[meal_label_qty] = [];

1 Answer 1

1

JavaScript does not have associative arrays; it has zero-indexed arrays, and objects with properties. It might seem like there are associative arrays, since you can access a property from an object using similar syntax (obj[property]).

To get the output you desire, just change meal_qty to an object instead of an array, or, restructure your code a bit and push a new object into the array instead of assigning it to an index.

// add the quantity label to the item object
item.qty = meal_label_qty;

// push the whole object onto the array
meal_qty.push(item);
Sign up to request clarification or add additional context in comments.

5 Comments

I did this var meal_label_qty = $(this).data("label-option"); // CREATE THE OBJECT WITH VALUES var item = { mon: $("#qty1").val(), tues: $("#qty2").val(), wed: $("#qty3").val(), thur: $("#qty4").val(), fri: $("#qty5").val() } item.qty = meal_label_qty; meal_qty.push(item); alert(meal_qty); i get [object] [Object]
The indexed array will always be a number "meal_label_qty" is always a number, so it should work
alert is just doing a toString on the meal_qty object, that's why you're seeing [object Object]. I'm a little unclear on what you are expecting to output when you log
Ok so I changed var meal_qty = {}; now I get Object {1001: Object, 1002: Object} Im guess that "object" is item, how can I fix this and make item show?
How to show them in your browser's console? Every browser does it a little differently. What are you using? If you console.log a single object, your dev tools should show you the properties/values inline.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.