这对我来说是一个简单但最痛苦的问题。
以以下代码段为例:
main
以上代码段的结果是:
const test = {};
test[2] = 'value2';
test[1] = 'value1';
console.log(test);
我想按原样传递此对象,并得到以下结果:
{
1: "value1",
2: "value2"
}
PS:我尝试了 Object.keys(test).sort()然后将其与用于重新创建对象的函数链接在一起,但似乎没有任何作用。
答案 0 :(得分:0)
使用正整数键是不可能的。使用非正整数键:[-2]
,[.2]
,['2 ']
等在大多数浏览器(但可能不是全部)上可以正常工作。
const obj = { '2 ': "value2", '1 ': "value1" };
console.log(obj);
console.log( JSON.stringify(obj, 0, 2).replace(/ ": "/g, '": "') );
答案 1 :(得分:0)
我检查一下,您可以使用类似的方法将键和值扩展到另一个对象中
{键:“”,值:“”}
class Program
{
static string connectionString = "Server=.;Database=test_sql_dependency;Integrated Security=True;";
static void Main(string[] args)
{
// 1. create database
// 2. enable service broker by executing this sql command on the database.
// alter database test_sql_dependency set enable_broker
// 3. start sql dependency, for some sql server connection string or with queue if you want.
//var queueName = "myFirstQueue";
//SqlDependency.Start(connectionString, queueName);
SqlDependency.Start(connectionString);
// complete the rest of the steps in seperate method to be able to call it again when you need to
// re-subscribe to the event again, becuase by default it will be executed only one time
RegisterSqlDependency();
Console.WriteLine("Listening to database changes...");
Console.ReadLine();
}
static void RegisterSqlDependency()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
if (connection.State != System.Data.ConnectionState.Open)
{
connection.Open();
}
// 4. create a sql command
// you can't say select *, and also you have to specefy the db owner (dbo.)
SqlCommand command = new SqlCommand("select Id, Name from dbo.Employee", connection);
// 5. create dependency and associtate it to the sql command
SqlDependency dependency = new SqlDependency(command);
// 6. subscribe to sql dependency event
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
// 7. execute the command
using (SqlDataReader reader = command.ExecuteReader())
{
}
}
}
static void OnDependencyChange(object sender, SqlNotificationEventArgs e)
{
var InsertOrUpdateOrDelte = e.Info;
//-----------------------------Finally-------------------------
// after you knew that there is a change happened
// you have to unsubscribe the event and execute the command again and then re-subscribe to the event
// 1. unsubscribe the event
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= OnDependencyChange;
// 2. re-subscribe to the event and execute the command again
RegisterSqlDependency();
}
}