I am trying to create a new object with nested value pairs. My initial challenge is that I have a huge object containing lots of junk data, and I'd like to strip away the value pairs that I do not need as well as renaming certain values. Performance is vital as this will iterate over thousands of items. I assume that creating a new object gives better performance than deleting and replacing values within the original object.
I've also put the following code in this JSFiddle: http://jsfiddle.net/mk3cH/2/
var data = [{
"ID": 20,
"Title": "Test item",
"Category": "1;#Local",
"ContentType": "Item",
"FileLeafRef": "20;#20_.000",
"FileRef": "20;#km/Lists/Meny/20_.000",
"_ModerationStatus": "0",
"_Level": "1",
"UniqueId": "20;#{447FEC10-E7E2-4561-A7B3-0433C8C90D31}",
"FSObjType": "20;#0",
"PermMask": "0x400001f07fff1bff",
"Modified": "2013-01-10 11:56:05",
"owshiddenversion": "8",
"Created": "2012-04-12 11:53:07"
}, {
"ID": 27,
"Title": "Test item 2",
"ContentType": "Item",
"FileLeafRef": "27;#27_.000",
"FileRef": "27;#km/Lists/Meny/27_.000",
"_ModerationStatus": "0",
"_Level": "1",
"UniqueId": "27;#{F7965302-AB39-45AE-9D9E-99BCF39F03B9}",
"FSObjType": "27;#0",
"PermMask": "0x400001f07fff1bff",
"Modified": "2013-04-14 14:00:30",
"owshiddenversion": "18",
"Created": "2012-04-20 14:39:02"
}]
var new_object = {}
var keysToIgnore = ['FileLeafRef', 'UniqueId', 'PermMask', 'owshiddenversion', 'Created', '_Level', '_ModerationStatus'].reduce(function (res, key) {
res[key] = true;
return res;
}, {});
var object_initiated = false
document.writeln("<b>Desired content of object: </b><br/>");
for (var i in data) {
for (var key in data[i]) {
if (key in keysToIgnore) continue;
if (key == "ID") {
new_object[i] = {
"ID": keydata
}
} else {
var keyname = key;
var keydata = data[i][key]
document.writeln(keyname + ": " + keydata + "<br/>");
new_object[i][keyname] = keydata
}
}
}
document.writeln("<br/><b>Actual new object content</b><br/>");
document.writeln(JSON.stringify(new_object));
"ID"
, what do you expect should happen? \$\endgroup\$ – 200_success Oct 19 '13 at 0:16