object-literal

how to filter data in array loop

ぐ巨炮叔叔 提交于 2019-12-23 16:59:49
问题 In my array is like this, var myColumnDefs = [ {a: "hh", b: "hh", c: "jk", d: "ggh", e: "hvh"}, {a: "dd", b: "gg", d: "nn", e: "rr", f: "jj"},..... ] I want to filter data and insert data in new array like this var newarray = {a,b,c,d,e,f} & another array var mysecondarray = [ {hh,hhjk,ggh,hvh}, {dd,gg,nm,rr,jj},.... ] 回答1: Assuming you want the result in arrays, then this should work. The result is in an object for further processing. var myColumnDefs = [{ a: "hh", b: "hh", c: "jk", d: "ggh"

Getting sibling value of key in a JavaScript object literal

廉价感情. 提交于 2019-12-23 16:40:19
问题 Does anybody know of there's a way to reference the value of a sibling key in a JavaScript object literal? so use the value of target in the beforeNext() function here: obj: { target: 'li.player a.icon-tag', parent: 'ul#drop_list', beforeNext: function(){ target.addClass('bind active'); } } 回答1: This is not a "JSON" object, but a JavaScript Object (or just "Object"). I assume that this is also contained in an Object literal as obj: { by itself is invalid syntax. Anyway, yes you can reference

what is this thing in JavaScript?

∥☆過路亽.° 提交于 2019-12-23 07:38:14
问题 var something = { wtf: null, omg: null }; My JavaScript knowledge is still horribly patchy since I last programmed with it, but I think I've relearned most of it now. Except for this. I don't recall ever seeing this before. What is it? And where can I learn more about it? 回答1: It is an object literal with two properties. Usually this is how people create associative arrays or hashes because JS doesn't natively support that data structure. Though note that it is still a fully-fledged object,

Literal object and function constructor

旧巷老猫 提交于 2019-12-23 05:48:07
问题 I took this test on http://jsperf.com/literal-obj-vs-function-obj and Literal wins on FF6, Opera 10, IE8, but the Function method faster on Chrome 13.0.782.112, so which one is a better method to use? var A = { aa : function(){ var i, j=[]; var arr = ['Literal', 'Function']; for (i = 0; i < arr.length; i++){ j[i] = arr[i]; } return j[0]; } }; var A1 = A; var A2 = A1; A1.foo = ' Test'; alert(A1.aa() + A2.foo); //Function test function B(){ this.bb = function(){ var i, j=[]; var arr = ['Literal

Creating new objects using object literals

左心房为你撑大大i 提交于 2019-12-22 18:13:06
问题 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? 回答1: 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:/

How to Sort a JS Object Literal?

℡╲_俬逩灬. 提交于 2019-12-21 08:23:45
问题 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 }; 回答1: 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

To count the number of objects in object literal using Jquery

一世执手 提交于 2019-12-20 14:17:52
问题 Code: var animals = { "elephant": { "name" : "Bingo", "age" : "4" }, "Lion": { "name" : "Tango", "age" : "8" }, "Tiger": { "name" : "Zango", "age" : "7" } } I want to count the number of objects using Jquery in this object literal. 回答1: You could use Object.keys(animals).length Or var count = 0; for (var animal in animals) { if (animals.hasOwnProperty(animal)) { count++; } } // `count` now holds the number of object literals in the `animals` variable Or one of many jQuery solutions that may

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

谁说胖子不能爱 提交于 2019-12-20 10:13:40
问题 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? 回答1: 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

JavaScript - passing an object literal as second arg to Object.create()

ぃ、小莉子 提交于 2019-12-19 03:54:14
问题 Referring to the JavaScript code snippet below, questions: Why does the object literal {item: {value: "foobar"}} behave differently when assigned to a variable (like in line 1) vs. when passed as an argument to Object.create() (like in line 5)? What is the difference between line 5 and line 8 - i.e. why is line 5 the correct way to pass the second argument to Object.create() and not line 8 (to override the item property in delegate)? Code Snippet: 1 var obj = {item: {value: "foobar"}}; 2

Call functions from function inside an object (object literal)

纵饮孤独 提交于 2019-12-17 22:34:21
问题 I'm learning to use object literals in JS, and I'm trying to get a function inside an object to run by calling it through another function in the same object. Why isn't the function "run" running when calling it from the function "init"? var runApp = { init: function(){ this.run() }, run: function() { alert("It's running!"); } }; 回答1: That code is only a declaration . You need to actually call the function: runApp.init(); Demo: http://jsfiddle.net/mattball/s6MJ5/ 回答2: There is nothing magical