jquery - How to store like objects in javascript -


I am trying to create a collection of objects in my JS code, and there are two ways that both are logical. I'm wondering if there is a 'best practice', and why I did not find much about it, though it seems a fundamental question.

Let's assume that they are the objects of color, the value of color is imaginary There are $ jQuery

Option 1: Identifier as property

I call 'database methods'; Each object looks like a table row and contains all fields related to that line

  color = [{name: "red", hex: "ff0000", hue: 34} Add color:  
  colors.push ({name: "name:" blue ", hex:" 0000ff ", hue: 100}]   

Purple ", hex:" ff00ff ", hue: 100}); Get color by specifying:

  redObjs = $ .grep (color, function (c) {return (c.name ==) =   

"Red");}); // array // simple way with 1 element? Hexofrex = red oz [0]. Hacks;

Specify the color:

  hueObjs = $ .grep (color, function (c) {return (c.hue = == 100);} ); // array hueNames = hueObjs.map (function (c) {return c.name;}) with 2 elements (color objects); // array with 2 elements (strings) Option 2: Identifier as key  

Here, a part of the information (identifier nl) is not stored in color Is the object.

  color = {red: {hex: "FF0000", color: 34} P color Add:  
  color ["purple"] = {hex: "ffffff", color: 100};   

Get color by specifying the name:

  redObj = colors ["red"]; Hexofred = red oz. Hex // or straight color ["red"]. Hex;   

Specify the color:

  hueNames = []; (In colors) {if (colors [c] .hue === 100) hueNames.push (c);} // array with 2 elements (strings)   

I can think of some (D) advantages for each, such as option 1 advantages: uniformity and database-similarity. An array, and not an object, is a more natural structure to store such objects that are evenly identical. Option 2 Benefits: At least, if the name is known, the ease and ease of retrieving information No duplicate color name possible

But it does not really help in taking me. What is the other logic to consider? What is considered a 'wrong' or bad behavior, and why? Does it matter if I am always getting the name of the color and it is never needed to see it (which seems to be the main problem for option 2)

Thanks guys!

It depends on the size of your data, and in most cases what kind of performance you want , Second optimization does not even matter, as long as your data lines do not start running in a few thousand or more. Try Jsperf.com benchmark in both cases, with a sample data set that you really want if peroformance is almost the same, go with the cleaner data format.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -