change structure of an object javascript - javascript

I want to change the structure of an object javascript, for example:
I have this structure :
obj = {
"email": "abc#site.com",
"societe.name": "xyz"
}
and I want to change it to :
obj = {
"email": "abc#site.com",
"societe": {
"name": "xyz"
}
}
thank's for help.

Try this:
var obj = {
"email": "abc#site.com",
"societe.name": "xyz"
};
var newObj = {};
var keys = Object.keys(obj);
for (var i = 0; i < keys.length ; i++) {
var key = keys[i];
// you can change this to '.name' if you want to be specific
if (key.indexOf('.') > -1) {
var splitted = key.split('.');
var innerObj = {};
innerObj[splitted[1]] = obj[key];
newObj[splitted[0]] = innerObj;
} else {
newObj[key] = obj[key];
}
}
console.log(newObj);
JSFIDDLE.

Related

javascript display all value of a json

I have this Json as a string:
{"Fromage":
{
"Bleu":"4€",
"Roquefort":"12€"
},
"Pain":
{
"Baguette":"1€"
},
"Yaourt":
{
"Faisselle":"2€"
},
"biere":"4€"
}
Using Javascript, how do I dispaly the name of all elements ?
My expected output is :
Fromage
Bleu
Roquefort
Pain
Baguette
Yaourt
Faisselle
biere
I tried this
const jsonStr = `
{"Fromage":
{
"Bleu":"4€",
"Roquefort":"12€"
},
"Pain":
{
"Baguette":"1€"
},
"Yaourt":
{
"Faisselle":"2€"
},
"biere":"4€"
}
`;
const value = JSON.parse(jsonStr);
for(var i = 0; i < Object.keys(value).length; i++){
var key = Object.keys(value)[i];
console.log(key);
for(var j = 0; j < Object.keys(value.key).length; j++){
var key2 = Object.keys(value.key)[j];
console.log(key2);
}
console.log("");
}
but it returns only first values and then undefined.
You should use Object.keys to get keys of direct children of that object.
Then call function recursively until you see a param obj doesn't have any child object.
Please run/check the following code.
const jsonStr = `
{"Fromage":
{
"Bleu":"4€",
"Roquefort":"12€"
},
"Pain":
{
"Baguette":"1€"
},
"Yaourt":
{
"Faisselle":"2€"
},
"biere":"4€"
}
`;
const jsonObj = JSON.parse(jsonStr);
const showKeys = (obj) => {
const keys = Object.keys(obj);
keys.forEach(key => {
console.log(key);
if (typeof obj[key] === 'object') {
showKeys(obj[key]);
console.log('');
}
});
};
showKeys(jsonObj);

Expand a dict key with dot as another dict

I have a dictionary looks like
var p = {
'name' : "John",
'personal.phone' : "123456",
'office.phone.number' : "321654",
'office.phone.extn': "4"
}
I want to convert the dict with doted keys expanded like
{
'name' : "John",
'personal' : {
'phone' : "123456",
}
'office' : {
'phone' : {
number : "321654",
extn : "4"
}
}
A function I wrote for expanding a single key. but its not exiting the loop
function expdictkey(str,v) {
str = str.split(".").reverse();
var p = {}
for (var i = 0; i < str.length; i++)
{
p[str[i]] = p
console.log(p)
}
return p;
}
expdictkey("phone.number.extn","4")
I am getting values from a from in the form of first dict, I want to convert it like second dict and put it in a model and save it. using backbone.js, and django rest server. in server the model is in the form of second dict.
Here you go:
var p = {
'name': "John",
'personal.phone': "123456",
'office.phone.number': "321654",
'office.phone.extn': "4"
}
function unwrap() {
var obj = {};
for (var index in p) {
var keys = index.split('.');
var value = p[index];
var ref = obj;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (keys.length - 1 === i) {
ref[key] = value;
} else {
if (ref[key] === undefined) {
ref[key] = {}
}
ref = ref[key]
}
}
}
return obj;
}
unwrap(p);

Readability of Javascript Array

I have a JSON OBJECT similar to following
{
"kay1":"value1",
"key2":"value2",
"key3":{
"key31":"value31",
"key32":"value32",
"key33":"value33"
}
}
I want to replace that with JSON ARRAY as follows
[
"value1",
"value2",
[
"value31",
"value32",
"value33"
]
]
My motivation to change the the JSON OBJECT to JSON ARRAY is it takes less amount of network traffic, getting the value from ARRAY is efficient than OBJECT, etc.
One problem I face is the readability of the ARRAY is very less than OBJECT.
Is there any way to improve the readability?
Here you go, I have written a function which will convert all instances of object to array and give you the result you are expecting.
var objActual = {
"key1":"value1",
"key2":"value2",
"key3":{
"key31":"value31",
"key32":"value32",
"key33": {
"key331" : "value331",
"key332" : "value332"
}
}
};
ObjectUtil = {
isObject : function(variable) {
if(Object.prototype.toString.call(variable) === '[object Object]') {
return true;
}
return false;
},
convertToArray : function(obj) {
var objkeys = Object.keys(obj);
var arr = [];
objkeys.forEach(function(key) {
var objectToPush;
if(ObjectUtil.isObject(obj[key])) {
objectToPush = ObjectUtil.convertToArray(obj[key]);
} else {
objectToPush = obj[key];
}
arr.push(objectToPush);
});
return arr;
}
};
var result = ObjectUtil.convertToArray(objActual);
console.log(result);
In my opinion, it should be:
{
"kay1":"value1",
"key2":"value2",
"key3":["value31", "value32", "value33"]
}
Using the init method is time critical. So use a scheme or assign static JSON to Storage.keys and assign your bulk data array to store.data. You can use store.get("key3.key31") after that. http://jsfiddle.net/2o411k00/
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
var data = {
"kay1":"value1",
"key2":"value2",
"key3":{
"key31":"value31",
"key32":"value32",
"key33":"value33"
}
}
var Storage = function(data){
this.rawData = data;
return this;
}
Storage.prototype.init = function(){
var self = this;
var index = 0;
var mp = function(dat, rootKey){
var res = Object.keys(dat).map(function(key, i) {
var v = dat[key];
if (typeof(v) === 'object'){
mp(v, key);
} else {
self.data.push(v);
var nspace = rootKey.split(".").concat([key]).join(".");
self.keys[nspace] = index++;
}
});
}
mp(this.rawData, "");
}
Storage.prototype.get = function(key){
return this.data[this.keys[key]];
};
Storage.prototype.data = [];
Storage.prototype.keys = {};
var store = new Storage(data);
console.log(data);
store.init();
console.log("keys", store.keys);
console.log("data", store.data);
console.log("kay1=", store.get(".kay1"));
console.log("key2=", store.get(".key2"));
console.log("key3.key31=", store.get("key3.key31"));
console.log("key3.key32=",store.get("key3.key32"));
console.log("key3.key33=", store.get("key3.key33"));

Implement dot notation Getter/Setter

I have an easy dot notation getter function and I would love to have a setter that works in the same way. Any ideas?
var person = {
name : {
first : 'Peter',
last : 'Smith'
}
};
// ---
var dotGet = function(str, obj) {
return str.split('.').reduce(function(obj, i) {
return obj[i];
}, obj);
};
var dotSet = function(str, value, obj) {
// updated, thx to #thg435
var arr = str.split('.');
while (arr.length > 1) {
obj = obj[arr.shift()];
}
obj[arr.shift()] = value;
return obj;
}
// returns `Peter`
var a = dotGet('person.name.first', person);
// should set `person.name.first` to 'Bob'
var b = dotSet('person.name.first', 'Bob', person);
var set = function (exp, value, scope) {
var levels = exp.split('.');
var max_level = levels.length - 1;
var target = scope;
levels.some(function (level, i) {
if (typeof level === 'undefined') {
return true;
}
if (i === max_level) {
target[level] = value;
} else {
var obj = target[level] || {};
target[level] = obj;
target = obj;
}
});
};
You can check out my expression compiler that does what you need and more.
The usage is:
var scope = {};
set('person.name', 'Josh', scope);
scope.person.name === 'Josh'
Try this:
var dotSet = function(str, value, obj) {
var keys = str.split('.');
var parent = obj;
for (var i = 0; i < keys.length - 1; i++) {
var key = keys[i];
if (!(key in parent)) {
parent[key] = {};
parent = parent[key];
}
}
parent[keys[keys.length - 1]] = value;
}
var person = {};
dotSet('person.name.first', 'Bob', person);
It produces this object:
{ person: { name: { first: 'Bob' } } }

Javascript Dynamic Grouping

I am trying to create a table, with some grouping headers.
All is well when i have 1 group, but how do i create dynamic groups?
This is what i have so far (for 1 group):
var groupby = '';
arrdata.each(function(element){
if (groupby != element.groupby) {
groupby = element.groupby;
tbody.push(groupby)
}
tbody.push(element)
})
How do i make it create dynamic groups?
You could group them into an Object first:
Array.prototype.groupBy = function(keyName) {
var res = {};
this.forEach(function(x) {
var k = x[keyName];
var v = res[k];
if (!v) v = res[k] = [];
v.push(x);
});
return res;
};
Then for .. in through the object:
var employees = [{first: ..., last: ..., job: ...}, ...];
var byJob = employees.groupBy('job');
for (var job in byJob) {
document.write('<h3>Job: '+job+'</h3>');
byJob[job].forEach(function(e) {
document.write('<p>'+e.last+', '+e.first+'</p>');
});
}
More generic solution. You can use it directly. Deep copy of objects used.
Please note that this solution is of polynomial time
Array.prototype.groupBy=function(property){
"use strict";function deepCopy(p) {
var c = {};
for (var i in p) {
if (typeof p[i] === 'object') {
c[i] = (p[i].constructor === Array)?[]:{};
deepCopy(p[i],c[i]);
}
else {
c[i] = p[i];
}
}
return c;
}
var retarr=[];
var len=this.length;
for(var i=0;i<len;i++){
var groupedlen=retarr.length,found=false;
for(var j=0;j<groupedlen;j++){
if(this[i][property]===retarr[j].key){
retarr[j].values.push(deepCopy(this[i]));
found=true;
break;
}
}
if (found === false) {
retarr.push({
key: this[i][property],
values: []
});
retarr[retarr.length-1].values.push(deepCopy(this[i]));
}
}
return retarr;
};
test case:
var arr=[{num: 1,key:911},{num: 2,key:22},{num: 3,key:23},{num: 4,key:222},{num: 4,key:2222},{num: 2,key:2},{num: 1,key:29},{num: 3,key:26},{num: 4,key:24}];
var grouped=arr.groupBy('num');
console.log(grouped);
try with this structure:
arrdata = [
{
"groupby": {fields},
"data":[{fields},{fields},{fields}]
},
{
"groupby": {fields},
"data":[{fields},{fields},{fields}]
},
{
"groupby": {fields},
"data":[{fields},{fields},{fields}]
},
]
jQuery.each(arrdata, function(element){
tbody.push(element.groupby)
jQuery.each(element.data , function(elementData){
tbody.push(elementData);
})
})

Categories

Resources