0

I wonder if this object code is seemly correct usage to have group1 emulate an array like? I would avoid using array to avoid error.

var group1={}
group1[0]={}
group1[0].fruits="Apple"
group1[0].qtystock1="1"
group1[1]={}
group1[1].fruits="Durian"
group1[1].qtystock4="2"
group1[2]={}
group1[2].fruits="Orange"
group1[2].qtystock1="3"
console.log(group1[0].qtystock4) //Should give "undefined"

4
  • Usage is correct, however not clear why you can't use array of objects. Commented Sep 24, 2015 at 20:47
  • An array of objects would suit this better Commented Sep 24, 2015 at 20:48
  • 2
    That code is valid, and the last line will log undefined as indicated. But what is the error you are trying to avoid? I'd use an array of objects... Commented Sep 24, 2015 at 20:48
  • I'm baffled on my other code which console.log(group1[0].y) is shown as 111 when the actual string for y is y: "111,222" using console.log(group1) I can't understand if it's chrome dev tool bug or a race condition. Commented Sep 24, 2015 at 20:57

2 Answers 2

2

It's correct, yes. Whether it's the right thing to do depends entirely on what else you're going to do with group1. If, for instance, you wanted to use the length property arrays have, then you wouldn't want to do that, because it won't have a length.

In general, if you're going to use something like an array, use an array; if you're going to use it as a non-array object, use an object. But in JavaScript, standard arrays are just objects with a bit of special behavior (details on my anemic little blog), so you can use an object like that if you want.

FWIW, here's the array version of that:

var group1 = [
    {fruits: "Apple", qtystock1: "1"},
    {fruits: "Durian", qtystock4: "2"},
    {fruits: "Orange", qtystock1: "3"}
];
console.log(group1[0].qtystock4); //Should give "undefined"

And a more concise version of the object you already have:

var group1 = {
    0: {fruits: "Apple", qtystock1: "1"},
    1: {fruits: "Durian", qtystock4: "2"},
    2: {fruits: "Orange", qtystock1: "3"}
};
console.log(group1[0].qtystock4); //Should give "undefined"

For the data as quoted, I'd probably use an array. I'd also probably use a consistent property name for qtystock1/qtystock4, but maybe it makes sense when you have the greater context.

Sign up to request clarification or add additional context in comments.

2 Comments

In your 2nd code block, weren't there some versions of Javascript that required a key that didn't start with a number if it wasn't in quotes? I see it works in the current Chrome, but I thought there was a reason to enclose the numeric property in quotes at some point?
@jfriend00: No, at least since the 3rd edition spec (1999; I'm not sure object initializers existed before then), the name in a property initializer has always been allowed to be Identifier, StringLiteral (either single- or double-quotes), or NumericLiteral.
1

This is legal Javascript and I see no errors in your code. If you assign an object a numeric property like this, the number is converted to a string (all property names in Javascript are strings) and you will end up with properties such as:

group1["0"]

which is interpreted by Javsacript the same as:

group1[0]

For code clarity, it probably makes more sense for you to use an array because then it will automatically maintain a .length property for you and you can use array functions on it like .forEach() and other iterators. You can still have properties like your .fruits on an array object as it is an object too.

So, an array is everything an object is plus it adds the array functionality. So, if you're using a zero-based sequence of items, you would usually gain clarity and functionality by using an array instead of a plain object and lose nothing.

As for your line of code:

console.log(group1[0].qtystock4) 

that will show undefined because group1[0] is an object which does not have a .qtystock4 property so JS will return undefined if you read it.

Comments

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.