物件、JSON 與拷貝

深淺拷貝、合併、遍歷、比較與 JSON。

20 / 20 題 · 依票數排序

7.6k

How do I remove a property from a JavaScript object?

Given an object: let myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; How do I remove the property regex to end up with the following myObject ? le…

2008javascriptobjectproperties·3 重點答案·3.4M 次瀏覽
4.5k

Why does Google prepend while(1); to their JSON responses?

Why does Google prepend while(1); to their (private) JSON responses? For example, here's a response while turning a calendar on and off in Google Calendar : while (1); [ ['u', [ […

2010javascriptjsonajaxsecurity·3 重點答案·612k 次瀏覽
3.9k

How do I test for an empty JavaScript object?

After an AJAX request, sometimes my application may return an empty object, like: var a = {}; How can I check whether that's the case?

2009javascriptjavascript-objects·3 重點答案·4.5M 次瀏覽
3.8k

How do I correctly clone a JavaScript object?

I have an object x . I'd like to copy it as object y , such that changes to y do not modify x . I realized that copying objects derived from built-in JavaScript objects will resul…

2009javascriptclonejavascript-objects·3 重點答案·2.6M 次瀏覽
3.6k

pretty-print JSON using JavaScript

How can I display JSON in an easy-to-read (for human readers) format? I'm looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc.

2011javascriptjsonpretty-print·3 重點答案·2M 次瀏覽
3.4k

How can I merge properties of two JavaScript objects?

I need to be able to merge two (very simple) JavaScript objects at runtime. For example I'd like to: var obj1 = { food: 'pizza', car: 'ford' } var obj2 = { animal: 'dog' } obj1.me…

2008javascriptjavascript-objects·3 重點答案·2.1M 次瀏覽
3.1k

Length of a JavaScript object

I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object? const myObject = new Object(); myObject["firstname"] = "Gareth"; my…

2008javascriptobjectjavascript-objects·3 重點答案·3.1M 次瀏覽
2.5k

What is JSONP, and why was it created?

I understand JSON, but not JSONP. Wikipedia's document on JSON is (was) the top search result for JSONP. It says this: JSONP or "JSON with padding" is a JSON extension wherein a p…

2010javascriptjsonjsonpterminology·3 重點答案·632k 次瀏覽
2.2k

How can I display a JavaScript object?

How do I display the content of a JavaScript object in a string format like when we alert a variable? The same formatted way I want to display an object.

2009javascriptserializationjavascript-objects·3 重點答案·2.6M 次瀏覽
2k

From an array of objects, extract value of a property as array

I have JavaScript object array with the following structure: objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ]; I want to extract a field from each object, an…

2013javascriptjavascript-objects·3 重點答案·2M 次瀏覽
1.8k

Is JavaScript a pass-by-reference or pass-by-value language?

The primitive types (number, string, etc.) are passed by value. Still, objects are unknown because they can be both passed by value (in which case we consider that a variable hold…

2009javascriptpass-by-referencepass-by-value·3 重點答案·598k 次瀏覽
1.8k

Parse JSON in JavaScript?

I want to parse a JSON string in JavaScript. The response is something like var response = '{"result":true,"count":1}'; How can I get the values result and count from this?

2011javascriptjsonparsing·3 重點答案·2M 次瀏覽
1.6k

What is the "right" JSON date format?

I've seen so many different standards for the JSON date format: "\"\\/Date(1335205592410)\\/\"" .NET JavaScriptSerializer "\"\\/Date(1335205592410-0500)\\/\"" .NET DataContractJso…

2012javascriptjson·3 重點答案·2.1M 次瀏覽
1.5k

Object comparison in JavaScript

What is the best way to compare objects in JavaScript? Example: var user1 = {name : "nerd", org: "dev"}; var user2 = {name : "nerd", org: "dev"}; var eq = user1 == user2; alert(eq…

2009javascriptobjectcomparisonobject-comparison·3 重點答案·1.6M 次瀏覽
1.5k

Safely turning a JSON string into an object

Given a string of JSON data, how can I safely turn that string into a JavaScript object? Obviously I can do this unsafely with something like: var obj = eval("(" + json + ')'); bu…

2008javascriptjsonparsing·3 重點答案·1.5M 次瀏覽