Tag: object literal

如何展平对象的文字属性?

我有一个遗留服务器返回的对象,我想通过JavaScript,jQuery甚至Underscore.js更改客户端的结构。 以下是我原始对象的样子: [ { “Id”:{ “LValue”:1, “Value”:1 }, “Date”:{ “LValue”:”2013-10-17T00:00:00″, “Value”:”2013-10-24T00:00:00″ }, “User”:{ “LValue”:508, “Value”:507 }, “Comments”:{ “LValue”:”This a test load”, “Value”:”This a test” }, “Name”:”John Doe”, “IsDeleted”:false } ] 但是在客户端,我想将它变平以获得“值”并将“LValues”填充到一个单独的属性中,所以如果我以后需要它,我不会松开它们: [ { “Id”:1, “Date”:”2013-10-24T00:00:00″, “User”:507, “Comments”:”This a test”, “Name”:”John Doe”, “IsDeleted”:false, “LValues”: { “Id”:1, “Date”:”2013-10-17T00:00:00″, “User”:508, “Comments”:”This a test load” } } ] 这将使对象的使用变得更加容易,任何帮助都将深深感激!

Javascript – 如何在对象文字的ajax调用中绑定’this’

我有一个对象文字router ,包含一个ajax调用。 我想在ajax调用中调用this.printMovies()其他函数,但这引用了ajax对象。 如何逃避它并使其引用router对象本身? 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(response, callback); //’this’ refers to ajax this.printMovies(response, callback).bind(this) //still doesn’t work } }, error: function […]

使用Jquery计算对象文字中的对象数

码: var animals = { “elephant”: { “name” : “Bingo”, “age” : “4” }, “Lion”: { “name” : “Tango”, “age” : “8” }, “Tiger”: { “name” : “Zango”, “age” : “7” } } 我想在此对象文字中使用Jquery计算对象的数量。