Javascript 如何使用随机字符串作为JSON对象名
我正在尝试创建一个JSON对象,名称为随机字符串,就像Firebase那样 目标:用随机字符串替换子名称。 例如:Javascript 如何使用随机字符串作为JSON对象名,javascript,json,Javascript,Json,我正在尝试创建一个JSON对象,名称为随机字符串,就像Firebase那样 目标:用随机字符串替换子名称。 例如: "Users" : { "mGKgNDOw0qd77m6tmdDh76zOQOm2" : { "email" : "someuser@gmail.com", "firstName" : "some", "lastName" : "user", "username" : "someuser" }, "vyMiCS
"Users" : {
"mGKgNDOw0qd77m6tmdDh76zOQOm2" : {
"email" : "someuser@gmail.com",
"firstName" : "some",
"lastName" : "user",
"username" : "someuser"
},
"vyMiCS7olNPh9bCXoKWqcIFNWVy2" : {
"email" : "someuser2@gmail.com",
"firstName" : "some",
"lastName" : "user2",
"username" : "someuser2"
}
}
这就是我到目前为止所得到的,我用一个字符串随机函数来控制我的大脑
randomString(length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
}
我想将我的数据推送到对象中,并使用随机字符串作为名称
我尝试了字符串插值,但不起作用
var expensesObject = {
uid: {
amount: spentAmount,
category: selectedCategory,
date: formattedDate,
time: formattedTime,
note: note
}
}
将对象
uid
复制到包含随机字符串的新对象
var费用对象={
uid:{
金额:5,
类别:“catA”,
日期:“2017年3月2日”,
时间:“14:00:00”,
注:“注”
}
};
var finalobject={};
设propertyName=randomString(10)//获取随机字符串
finalobject[propertyName]=expensesObject.uid//复制uid对象
console.log(finalobject);
函数随机字符串(长度){
返回Math.round((Math.pow(36,长度+1)-Math.random()*Math.pow(36,长度)).toString(36.slice(1);
}
您可以通过使用[]
直接设置对象的键来完成此操作:
var expensesObject = {}
expensesObject[uid] = {
amount: spentAmount,
category: selectedCategory,
date: formattedDate,
time: formattedTime,
note: note
}
你可以这样做:
function randomString(length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
}
var keylength = 16; // <-- change as needed
var myObject = {
amount: spentAmount,
category: selectedCategory,
date: formattedDate,
time: formattedTime,
note: note
}
var expensesObject = {};
var uuid = randomString(keylength);
expensesObject[uuid]=myObject; // <-- use [ var_name ]
var expensesObjectStr = "{"+ '"'+ uid+'"'+": {"+
'"amount"'+":"+ spentAmount+","+
'"category"'+":"+'"'+ selectedCategory+'"'+","+
'"date"'+": "+'"'+ formattedDate+'"'+","+
'"time"'+": "+'"'+ formattedTime+'"'+","+
'"note"'+": "+'"'+ note+'"'+
"} }";
var obj = jQuery.parseJSON(expensesObjectStr);
函数随机字符串(长度){
返回Math.round((Math.pow(36,长度+1)-Math.random()*Math.pow(36,长度)).toString(36.slice(1);
}
var keylength=16;//<代码>var temp=支出对象[uid];
expensesObject[randomString()]=temp;
考虑一下这个代码
var users = {}
users[ randomString(20) ] = {
amount : spentAmount,
category : selectedCategory,
date : formattedDate,
time : formattedTime,
note : note
}
您可以使用单个随机数来表示不同的位置。检查是小写还是大写 然后将
uid
的属性分配给新键并删除该属性
函数replaceUID(对象){
object[random(28)]=object.uid;
删除object.uid;
}
函数随机(大小){
var r,s='';
而(s.长度<尺寸){
r=数学地板(数学随机()*62);
s+=r>=36?(r-26).toString(36).toUpperCase():r.toString(36);
}
返回s;
}
var expensesObject={uid:{amount:'spentAmount',类别:'selectedCategory',
日期:'formattedDate',时间:'formattedTime',注释:'note'};
替换UID(支出对象);
控制台日志(expensesObject)代码>您还可以创建字符串并将其转换为JSON对象,如下所示:
function randomString(length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
}
var keylength = 16; // <-- change as needed
var myObject = {
amount: spentAmount,
category: selectedCategory,
date: formattedDate,
time: formattedTime,
note: note
}
var expensesObject = {};
var uuid = randomString(keylength);
expensesObject[uuid]=myObject; // <-- use [ var_name ]
var expensesObjectStr = "{"+ '"'+ uid+'"'+": {"+
'"amount"'+":"+ spentAmount+","+
'"category"'+":"+'"'+ selectedCategory+'"'+","+
'"date"'+": "+'"'+ formattedDate+'"'+","+
'"time"'+": "+'"'+ formattedTime+'"'+","+
'"note"'+": "+'"'+ note+'"'+
"} }";
var obj = jQuery.parseJSON(expensesObjectStr);
这里是小提琴-你想用随机数交换uid
,对吗?@Kinduser是的,你知道怎么做吗?可能是重复的,所以你的问题实际上是生成随机数,或者在生成后将其用作键?可能是重复的,我不认为OP的意思是将uid
替换为现有对象上的现有属性名。最后一个代码示例中的对象只是所需代码的一个示例output@Weedoze这正是我要找的!如果我们要复制uid对象,性能会下降吗?我在处理大数据set@J.Doe你可以自己试试,但我想不会慢的。你管大的叫什么?如果答案解决了你的问题,别忘了给答案投票或做标记problem@Weedoze我正在尝试一个费用应用程序,用户可以每天添加费用对象。将在一秒钟内接受您的答案,所以现在还不让我这么做:)我不是问如何生成随机字符串:)很好的方法!谢谢欢迎来到Stack!!你能给我们解释一下吗?非常感谢。