0

I have an array with keys and values which elements i want to push into 2 different arrays (key into one and value into another)

here is the code i have used:

// the array i want to split
var defaultc = {
    'a':'r019',
    'b':'r027',
    'c':'r027',
    'd':'r027',
    'e':'r047'
};
// the array i want to add to
var step_layer = []; // keys to this
var step_colour = {}; // values to this


$('#reset').click(function() {
    $.each(defaultc, function(layer, colour) {

        // both these functions are working properly
        apply_changes(layer, colour);
        sumary(layer, colour);
    });

    // this part is not
    for (var layer in defaultc) {
        if (!defaultc.hasOwnProperty(layer)) {
        step_layer.push(layer);
        step_colour[layer].push(colour);
    }}
    return false;
});
0

2 Answers 2

2
var defaultc = {
    'a':'r019',
    'b':'r027',
    'c':'r027',
    'd':'r027',
    'e':'r047'
};

var step_layer = []; // keys to this
var step_colour = []; // values to this

for(var key in defaultc) {
   setip_layer.push(key);
   step_colour.push(defaultc[key]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@thecodeparadox: Your code is wrong. step_colour is an object and has no push method.
1

You have an Object, not an Array. Its quite simple:

var step_layer = Object.keys(defaultc); // array of property names
var step_colour = Object.values(defaultc); // arrray of property values

But as .values is a non-standard method, .keys only available on modern browsers and the iteration order of object properties is not fix, you should use a single loop over the object:

var step_layer = []; // array of property names
var step_colour = []; // arrray of property values
for (var layer in defaultc) {
    step_layer.push(layer);
    step_colour.push(defaultc[layer]);
}

And that's where your code differs. First, you start with a var step_colour = {};. This is a object again, to be used for a key-value-map. So, it even would be possible to access a property of it with step_colour[layer], but this is still undefined. You seem to assume that it would be an array, using .push on it - that will throw an error (look into your debugging console). Im not sure what you want to achieve with that. Creating an array, cloning the defaultc variable?

Another problem is if (!defaultc.hasOwnProperty(layer)). First, your defaultc is a plain object and does not inherit any enumerable properties, so just leave that condition away. Second, as all layers you loop over are own properties of defaultc, the block will never get executed.

5 Comments

@Bergi i understand what you are saying.. i was experimenting it now. but step_layer requires to be an object for my other operations to work. Im exploring ways to push it to an object or push it to an array and then convert that array to object.
And of which keys and values should it consist? What would be the difference to "defaultc"?
@Bergi the keys of defaultc should be added to step_layer as '1':'a', '2':'b'.. im a newbie to coding so im trying to learn it.. sorry for not making things clear
@Bergi is there a good place where i can find out whats an array and whats an object?
Not really, at least I found no "good" one. You might read the quirksmode intro, Mastering Javascript Arrays and about misusing Arrays as associative arrays...

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.