object-literal

Why leave a trailing comma after a key value pair in an object literal?

烈酒焚心 提交于 2019-12-08 10:12:08
问题 I was looking at gruntjs and I looked at some JSON examples used to configure Grunt tasks. Here is an example of the JSON: grunt.initConfig({ concat: { foo: { // concat task "foo" target options and files go here. }, bar: { // concat task "bar" target options and files go here. }, }, uglify: { bar: { // uglify task "bar" target options and files go here. }, }, }); As you can see, there is an 'extra' comma after each of the bar properties. I tried this notation in Chrome and it is valid.

How do I slice an array from an array of object literals?

别等时光非礼了梦想. 提交于 2019-12-07 06:43:14
问题 I have this array, in which each index contains an object literal. All of the object literals have the same properties. Some of the object literals have the same value for a given property, and I want to create a new array containing only those object literals. My idea is to sort the array, and slice it into a new array... Here is the array: var arr = []; arr[0] = { country: "United States", num: 27 }; arr[1] = { country: "Australia", num: 5 }; arr[2] = { country: "United States", num: 7 };

Update/Create object properties dynamically

梦想与她 提交于 2019-12-06 13:29:36
问题 I'm creating a feature that provides a get() and set() function to read and update values from a JavaScript object. The trick is that I'm using a dot notated string to specify the properties. I've got the get() working fine, it's the set() that I'm having troubles with. Sample data: var data = { person: { name: 'Fred', birthday: '19840101', address: { street: '123 Main St.', city: 'Anytown', state: 'NY', zip: '123123' }, family: { spouse: 'Sally', children: ['Sam', 'Frank', 'Susan'] } } };

Creating new objects using object literals

半城伤御伤魂 提交于 2019-12-06 11:08:43
I have the following object literal: var a = {a:1,b:2} now I want another instance of the same object. If I'm using a constructor, I can do this using the 'new' operator, ie: b = new a(); How to create a new instance of an object using object literals? The simplest way would be with Object.create var b = Object.create(a); console.log(b.a); //1 console.log(b.b); //2 DEMO And of course if you need to support older browsers, you can get the MDN shim for Object.create here 来源: https://stackoverflow.com/questions/9205136/creating-new-objects-using-object-literals

How do I slice an array from an array of object literals?

雨燕双飞 提交于 2019-12-05 10:40:34
I have this array, in which each index contains an object literal. All of the object literals have the same properties. Some of the object literals have the same value for a given property, and I want to create a new array containing only those object literals. My idea is to sort the array, and slice it into a new array... Here is the array: var arr = []; arr[0] = { country: "United States", num: 27 }; arr[1] = { country: "Australia", num: 5 }; arr[2] = { country: "United States", num: 7 }; So, I want to create a new array containing only those objects where the property country is "United

How to access an outer member from a nested object literal?

独自空忆成欢 提交于 2019-12-04 04:12:08
In the following code, can the x member be accessed from the nested object literal? var outer = { x : 0, inner: { a : x + 1, // 'x' is undefined. b : outer.x + 1, // 'outer' is undefined. c : this.x + 1 // This doesn't produce an error, } // but outer.inner.c is NaN. } In the way you put it - no. You need two stages construction, this will work: var outer = { x : 0 }; // outer is constructed at this point. outer.inner = { b : outer.x + 1 // 'outer' is defined here. }; Not in the construct you have there, no. The main reason being that outer doesn't actually exist yet when you are inside inner

How to Sort a JS Object Literal?

不想你离开。 提交于 2019-12-04 02:17:20
If I have this JS object literal: var foo = { Sussy: 4, Billy: 5, Jimmy: 2, Sally: 1 }; How can I create a new, sorted object literal: var bar = { Sally: 1, Jimmy: 2, Sussy: 4, Billy: 5 }; Re: How to sort a JS Object? Answer: You can't. So instead, you need a more sophisticated data structure. You have many options: You can use a separate array to hold the order of the object's keys. (This is what @Felix Kling's answer demonstrates.) Good: fast retrieval via order or name. Bad: needs a second data structure that must be kept synched with the first. Instead of the Object simply holding

How to fill a Javascript object literal with many static key/value pairs efficiently?

我的梦境 提交于 2019-12-03 06:28:49
问题 The typical way of creating a Javascript object is the following: var map = new Object(); map[myKey1] = myObj1; map[myKey2] = myObj2; I need to create such a map where both keys and values are Strings. I have a large but static set of pairs to add to the map. Is there any way to perform something like this in Javascript: var map = { { "aaa", "rrr" }, { "bbb", "ppp" } ... }; or do I have to perform something like this for each entry: map["aaa"]="rrr"; map["bbb"]="ppp"; ... Basically, remaining

How to determine if an object is an object literal in Javascript?

 ̄綄美尐妖づ 提交于 2019-12-02 20:42:11
Is there any way to determine in Javascript if an object was created using object-literal notation or using a constructor method? It seems to me that you just access it's parent object, but if the object you are passing in doesn't have a reference to it's parent, I don't think you can tell this, can you? I just came across this question and thread during a sweet hackfest that involved a grail quest for evaluating whether an object was created with {} or new Object() (i still havent figured that out.) Anyway, I was suprised to find the similarity between the isObjectLiteral() function posted

How to fill a Javascript object literal with many static key/value pairs efficiently?

≡放荡痞女 提交于 2019-12-02 19:02:52
The typical way of creating a Javascript object is the following: var map = new Object(); map[myKey1] = myObj1; map[myKey2] = myObj2; I need to create such a map where both keys and values are Strings. I have a large but static set of pairs to add to the map. Is there any way to perform something like this in Javascript: var map = { { "aaa", "rrr" }, { "bbb", "ppp" } ... }; or do I have to perform something like this for each entry: map["aaa"]="rrr"; map["bbb"]="ppp"; ... Basically, remaining Javascript code will loop over this map and extract values according to criterias known 'at runtime'.