object-literal

Javascript data structure for fast lookup and ordered looping?

和自甴很熟 提交于 2019-12-17 22:08:29
问题 is there a data structure or a pattern in Javascript that can be used for both fast lookup (by key, as with associative arrays) and for ordered looping? Right, now I am using object literals to store my data but I just disovered that Chrome does not maintain the order when looping over the property names. Is there a common way to solve this in Javascript? Thanks for any hints. 回答1: Create a data structure yourselves. Store the ordering in an array that is internal to the structure. Store the

Sort array of objects

末鹿安然 提交于 2019-12-17 22:01:46
问题 I have an array of object literals like this: var myArr = []; myArr[0] = { 'score': 4, 'name': 'foo' } myArr[1] = { 'score': 1, 'name': 'bar' } myArr[2] = { 'score': 3, 'name': 'foobar' } How would I sort the array so it ascends by the 'score' parameter such that it would change to: myArr[0] = { 'score': 1, 'name': 'bar' } myArr[1] = { 'score': 3, 'name': 'foobar' } myArr[2] = { 'score': 4, 'name': 'foo' } Thanks in advance. 回答1: Try myArr.sort(function (a, b) {return a.score - b.score}); The

Is this Javascript object literal key restriction strictly due to parsing?

▼魔方 西西 提交于 2019-12-17 18:57:41
问题 Please refer to the code below, when I "comment in" either of the commented out lines, it causes the error (in IE) of "':' expected". So then is my conclusion correct, that this inability to provide a reference to an object value, as an object key in a string literal; is this strictly an interpreter/parsing issue? Is this a candidate for an awful (or at least "bad") "part" of Javascript, in contrast to Crockford's "good parts"? <script> var keys = {'ONE': 'one'}; //causes error: //var obj1 =

dynamic keys for object literals in Javascript [duplicate]

落花浮王杯 提交于 2019-12-16 21:00:34
问题 This question already has answers here : How to use a variable for a key in a JavaScript object literal? (13 answers) Closed last month . Ok so I'm working away on a project in Nodes, and I've come across a small problem with the keys in object literals, I have the following set-up: var required = { directories : { this.applicationPath : "Application " + this.application + " does not exists", this.applicationPath + "/configs" : "Application config folder does not exists", this.applicationPath

Javascript Literal Object Notation This vs Object Name

社会主义新天地 提交于 2019-12-13 15:56:17
问题 I have an object literal like this: var test = { one: function() { }, two: function() { this.one(); // call 1 test.one(); // call 2 } }; What is the difference between the calls in the two function (using the object literal name versus using this )? 回答1: test is always bound within the closure of the two function to the variable test whereas this depends on how the function is called. If the function is called using the regular object member access syntax, this becomes the object owning the

Does JavaScript hoist if statements when object literals are involved?

感情迁移 提交于 2019-12-13 09:53:26
问题 var foo = {}; document.body.innerHTML = console.log = location.hash = 'Hi ' + '<br> ' + foo.bar + '<br> ' + foo.baz; setTimeout(function() { foo.baz = foo["bar"] = []; foo.bar.push(new Date); foo.baz.push(new Date); document.body.innerHTML = console.log = location.hash = 'Hi ' + '<br> ' + foo.bar + '<br> ' + foo.baz}, 5000); 回答1: Since you set node.bar equal to false , (node.foo && node.bar) will evaluate to false whether the properties are attached or not. Rather than checking if those

Using jQuery deferred and promise inside loop

為{幸葍}努か 提交于 2019-12-13 03:10:11
问题 This is what i am trying to achieve, I have a sorted array which i pass to jQuery each. Inside each there is an ajax call which will fetch me the desired data and every time push into another array(lets call it allJsonData). Finally i display allJsonData. Problem is whenever i display allJsonData, elements are always displayed inconsistently (not alphabetically/random order). I am expecting allJsonData to be displayed alphabetically (that is AList data first, BList data second, CList data

Javascript - how to bind 'this' inside an ajax call to the object literal

倾然丶 夕夏残阳落幕 提交于 2019-12-12 17:29:17
问题 I have an object literal router , containing an ajax call. I want to call other functions this.printMovies() inside the ajax call but this refer to the ajax object. How do I escape it and make this refer to the router object itself? var router = { //... init : function() { this.getData("api/movies", "movies", callback); }, getData : function (url, htmlType, callback) { $.ajax({ url: url, dataType: 'json', success: function (response) { if (response && response.length > 0) { this.printMovies

Add different value to the same object literal javascript

自古美人都是妖i 提交于 2019-12-12 02:06:31
问题 I have a piece of code to create an object literal array. The array is created from 2 other string array, one will become the object literal colHeads and the other array will be the data dataArr . colHeads = [name, state] dataArr = [John A. Smith,Joan B. Jones] var temp = []; var tempObj = {}; for (var i=0; i<colHeads.length; ++i) { // columns var dataArr = colDatas[i].split(","); for (var j = 0; j < dataArr.length; j++) { // data tempObj[colHeads[i]] = dataArr[j]; } temp.push(tempObj); } The

How to convert a string containing dots to a property accessor in javascript?

梦想的初衷 提交于 2019-12-11 19:14:17
问题 For example, if I have a string: var foo = 'a.b.c'; ... and Object: var bar = { a: { b: { c: null } } } How can I use the string to set the value of 'c' to 'Hello World!"? 回答1: Here's one of those not so simple or consistent ways var bar = { a: { b: { c: 'test' } } } var foo = 'a.b.c', arr = foo.split('.'), o = bar; arr.forEach(function(key, i) { if (i === arr.length-1) { o[key] = 'Hello World' }else{ o = o[key]; } }); FIDDLE 回答2: Another possible solution is to use eval, but it is unsafe and