gpt4 book ai didi

javascript - 如何在缓存中放入和读取复杂对象?

转载 作者:行者123 更新时间:2023-11-28 00:43:09 24 4
gpt4 key购买 nike

我有以下代码,它添加了一个 JavaScript 复杂对象(树),您可以将其视为数组的数组,其值本身就是对象。

我想像这样放入缓存:

localStorage.Terms = tree;

但是在读取它时,在控制台中,它显示为object object

function GetTermsDataFromTaxonomy(){
var start = new Date().getTime();
//Current Context
var context = SP.ClientContext.get_current();
//Current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Term Stores
var termStores = taxSession.get_termStores();
//Name of the Term Store from which to get the Terms.
var termStore = termStores.getByName("Taxonomy_kl5tZjInn7STsFTzIE7n3Q==");
//GUID of Term Set from which to get the Terms.
var termSet = termStore.getTermSet("31da4bc1-6429-499a-9d5e-be5e18b13c87");
var terms = termSet.getAllTerms();
context.load(terms);

context.executeQueryAsync(function(){
var termEnumerator = terms.getEnumerator(), tree = {
term: terms,
children: []
};
var termList = "Terms: \n";
while(termEnumerator.moveNext()){
var currentTerm = termEnumerator.get_current();
var currentTermPath = currentTerm.get_pathOfTerm().split(';');
var children = tree.children;

// Loop through each part of the path
for (var i = 0; i < currentTermPath.length; i++) {
var foundNode = false;
for (var j = 0; j < children.length; j++) {
if (children[j].name === currentTermPath[i]) {
foundNode = true;
break;
}
}

// Select the node, otherwise create a new one
var term = foundNode ? children[j] : { name: currentTermPath[i], children: [] };

// If we're a child element, add the term properties
if (i === currentTermPath.length - 1) {
term.term = currentTerm;
term.title = currentTerm.get_name();
term.guid = currentTerm.get_id().toString();
}

// If the node did exist, let's look there next iteration
if (foundNode) {
children = term.children;
}
// If the segment of path does not exist, create it
else {
children.push(term);

// Reset the children pointer to add there next iteration
if (i !== currentTermPath.length - 1) {
children = term.children;
}
}
}
}
localStorage.Terms = tree;
var end = new Date().getTime();
var time = end - start;
console.log('Execution time: ' + time);
},
function(sender,args){
console.log(args.get_message());
});
}


function GetAllTermsRecursive(){
if(typeof(Storage) !== "undefined")
{
var lastRefreshDateFromTermStore = localStorage.LastRefreshDateFromTermStore;

//get data when the code is executed
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
var today = new Date(year, month, day, 0,0,0,0);

if(lastRefreshDateFromTermStore < today){
localStorage.removeItem('Terms');
localStorage.removeItem('LastRefreshDateFromTermStore');
}

//Check if data has been cached.
if(typeof(localStorage.Terms) !== "undefined")
{
var start = new Date().getTime();
var terms=localStorage.Terms;
var end = new Date().getTime();
var time = end - start;
console.log('Execution time with cache: ' + time);
}
else
{
//get date for when the data is cached
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
var cachedDate = new Date(year, month, day, 0,0,0,0);

localStorage.LastRefreshDateFromTermStore = cachedDate;
GetTermsDataFromTaxonomy();
}
}
else {
alert("Cache not sopported in old browsers, please upgrade");
}
}

最佳答案

您只能在 localStorage 中存储字符串值。每当您尝试保存 Object 类型时,都会调用其 toString 方法,这当然会导致 [object Object]

通常,您使用 JSON.stringify 来保存数据(对象的字符串表示形式)

localStorage.Terms = JSON.stringify(tree);
当您检索数据时

JSON.parse:

var terms = JSON.parse(localStorage.Terms);

关于javascript - 如何在缓存中放入和读取复杂对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27712935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com