0

I have an object containing arrays that I want to return only a select part. underscore.js is installed and expecting it to provide the tools I need.

Here is the object called makes.

 { 2011: [ "Chevy", "Ford" ], 2012: [ "Chevy", "Ford", "GMC", "Hyundai" ] }

I've tried this and it returns an empty array.

chyear = 2012;
var makesbyyear = _.toArray(makes.chyear);
  • 2
    To get the reference `var makesbyyear = makes[chyear];` or for new array `var makesbyyear = makes[chyear].slice();` – Pranav C Balan Jul 01 '16 at 15:46
  • chyear is not a parameter of makes. You would need to use makes[chyear] and probably set chyear to text instead; chyear = '2012'; – ManoDestra Jul 01 '16 at 16:05
  • Have a look at underscore's [pick](http://underscorejs.org/#pick) which may be useful if you want to get more than one year at the same time e.g. `_.pick(data, '2012')` or `_.pick(data, '2012, '2013')` – Gruff Bunny Jul 01 '16 at 16:35
  • Thanks everybody. Part of my problem is probably [bracket] vs (braces) or array vs json object. – artdudejoe Jul 01 '16 at 17:06
  • This is not JSON (which is a string-based format for exchanging information, such as between the browser and web servers). It is not a multi-dimensional array, and it's not even a multi-dimensional object. It's just a plain old object containing arrays as values. Accessing a property on an object is a rudimentary feature of JS, and well documented in any tutorial, such as https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties--take a look. And you definitely don't need underscore to retrieve a property from an object. –  Jul 01 '16 at 17:31
  • @ManoDestra Why would I need to say `chyear = '2012';`?? –  Jul 01 '16 at 17:34
  • @torazaburo Because it's a property name of that object. The conversion to text may be implicit, if it works, but better to be explicit in terms of the data type. – ManoDestra Jul 01 '16 at 18:10
  • @ManoDestra What do you mean by "if it works"--why wouldn't it work? It doesn't matter and is not necessary. In what way is it "better" to be explicit? –  Jul 01 '16 at 18:44
  • @torazaburo Because you're accessing a string property via a number, which has an implicit conversion. And it's always better to be EXPLICIT in your code. Try this bit of code and see what it outputs as the type for your 2012 key in makes: `for (var key in makes) { console.log(key, typeof key); }`. It's a string, not a number. No matter how you set a property in an object, it's key will be a string. Even 212.232. – ManoDestra Jul 01 '16 at 19:12
  • This was about formatting data correctly and accessing it with the proper usage. Ended up with `var makes = { "2011": { "ch":"Chevy", "fd":"Ford" }, "2012": { "ch":"Chevy", "fd":"Ford", "gm":"GMC", "hy":"Hyundai" } }`. Note the braces, explicit declarations, and keys for every value. Then accessed it with `makes["2012"]`. – artdudejoe Jul 01 '16 at 20:44

1 Answers1

0

simply extract the required property as if it were an index.

 chyear = 2012;
 var makesbyyear = makes[chyear];

in JavaScript, objects have properties. A property is kind of a variable inside the object. So, properties are similar to simple variables, but are attached, so you need to access them through their "container" object.

Usually you access the properties of an object using a dot notation

 object.property

But properties can also be accessed using a bracket notation, like indices of an array. That's why objects are sometimes known also as associative arrays, since each property is interchangeable with an index value.

object["property"]
PA.
  • 28,486
  • 9
  • 71
  • 95
  • chyear should probably be text too, rather than a number. The conversion may well be explicit, but it's worth noting, as it's a parameter name here :) – ManoDestra Jul 01 '16 at 16:06
  • Actually, objects are **not** known as associative arrays. That's a term used in other languages, not JavaScript. –  Jul 01 '16 at 17:34
  • @todazaburo objects used (or abused) via indices, are *sometimes* called in javascript associative arrays too. – PA. Jul 01 '16 at 17:47
  • 1
    @manodestra yes, they are most likely text, as my generic example mentions. But, OP's question is using a number. – PA. Jul 01 '16 at 17:51
  • Yes, they are sometimes called associative arrays, by people who don't know better, and doing so is confusing and incorrect. Please show me where to find the word "associative array" in the JS spec. –  Jul 01 '16 at 18:47