如何使用随机字符串作为JSON对象名称

时间:2017-03-02 13:16:52

标签: javascript json

我正在尝试使用随机字符串作为名称创建一个JSON对象,就像Firebase一样。

目标:使用随机字符串替换子名称。

例如:

"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
  }
}

7 个答案:

答案 0 :(得分:1)

您可以使用[]直接设置对象的密钥来执行此操作:

var expensesObject = {}

expensesObject[uid] = {
    amount: spentAmount,
    category: selectedCategory,
    date: formattedDate,
    time: formattedTime,
    note: note
}

答案 1 :(得分:1)

考虑这段代码

var users = {}

users[ randomString(20) ] = {
    amount   : spentAmount,
    category : selectedCategory,
    date     : formattedDate,
    time     : formattedTime,
    note     : note
}

答案 2 :(得分:1)

您可以将一个随机数用于不同的地方。检查是大写还是大写。

然后为新密钥分配uid的属性并删除属性。



function replaceUID(object) {
    object[random(28)] = object.uid;
    delete object.uid;
}

function random(size) {
    var r, s = '';
    while (s.length < size) {
        r = Math.floor(Math.random() * 62);
        s += r >= 36 ? (r - 26).toString(36).toUpperCase() : r.toString(36);
    }
    return s;
}

var expensesObject = { uid: { amount: 'spentAmount', category: 'selectedCategory',
date: 'formattedDate', time: 'formattedTime', note: 'note' } };

replaceUID(expensesObject);
console.log(expensesObject);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

将对象uid复制到包含随机字符串的新对象

&#13;
&#13;
var expensesObject = {
  uid: {
    amount: 5,
    category: "catA",
    date: "02-03-2017",
    time: "14:00:00",
    note: "Notes"
  }
};

var finalobject = {};
let propertyName = randomString(10); //get the random string
finalobject[propertyName] = expensesObject.uid; //copy uid object
console.log(finalobject);

function randomString(length) {
  return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
}
&#13;
&#13;
&#13;

答案 4 :(得分:0)

你可以这样做:

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 ]

答案 5 :(得分:0)

您还可以创建字符串并将其转换为JSON对象,如下所示:

var expensesObjectStr = "{"+ '"'+ uid+'"'+": {"+
    '"amount"'+":"+ spentAmount+","+
    '"category"'+":"+'"'+ selectedCategory+'"'+","+
    '"date"'+": "+'"'+ formattedDate+'"'+","+
    '"time"'+": "+'"'+ formattedTime+'"'+","+
    '"note"'+": "+'"'+ note+'"'+
  "} }";

var obj = jQuery.parseJSON(expensesObjectStr);

这里小小提琴 - https://jsfiddle.net/skyr9999/2dvffaty/

答案 6 :(得分:-2)

var temp = expensesObject[uid];
expensesObject[randomString(<length>)] = temp;