Irvan Hilmi
Irvan Hilmi

Reputation: 491

Javascript Object Behavior Won't Assign Value

Trying to assign at anonymous object but the value became 'Array(0)'

I try to console the values below console.log(values, { state: state, values: values })

And got this result

[inpid: 'asd', inppwd: 'asd']
{state: 4, values: Array(0)}

Why the values in the object became Array(0)?

When i console.log(values, state) I got this result

[inpid: 'asd', inppwd: 'asd']
4

Here i implement the code

function getInputsValue() {
    let state = 4;
    let values = [];

    ... some code that make `values` became `[inpid: 'asd', inppwd: 'asd']`

    return { state: state, values: values }
}

any explaination? please

Upvotes: 0

Views: 73

Answers (1)

Roe
Roe

Reputation: 858

That's how Chrome displays a 0-length array in value summaries in the console. Empty arrays can still contain properties.

You are trying to add properties to an array, I recommend using plain objects instead. You can initialize the object like this:

let values = {
    inpId: '',
    inpPwd: ''
};

You can simply access the values by doing this.

values.inpId;
values.inpPwd;

This allows you to get and set the values within. The rest of the code doesnt need to be changed unless you iterate over the values array.

Side note

If you need the values object to be an array. You can simply change it to an array and add the objects into it.

Upvotes: 2

Related Questions