Actually i figure it out how to convert all items inside an object or an array. Let me show you my way:
var copyData = {};
var data = {
"1":[["input","Was spielt Jakob gern?"],["input sag alt",["Jakob", "Er", " spielt nicht gern Basketball."]]],
"2":[["input alt",["Lena", "Sie", " chattet gern."]],["input sag","Was macht Lena nicht gern?"]],
"3":[["input alt",["Meike", "Sie", " sieht gern fern."]],["input sag","Was macht Meike nicht gern?"]],
"4":[["input","Was spielt Tim gern?"],["input sag alt",["Tim", "Er", " spielt nicht gern Fußball."]]],
"5":[["input","Was spielt Simon gern?"],["input sag","Was spielt Simon nicht gern?"]],
"6":[["input alt",["Melanie", "Sie", " tanzt gern Hip Hop."]],["input sag","Was tanzt Melanie nicht gern?"]]
};
My Object is the one named data. Here is my for loop's to convert this object into another with each element encoded.
for(var i in data){
copyData[i] = [];
for(var j in data[i]){
copyData[i][j] = [];
for(var g in data[i][j]){
if(typeof data[i][j][g] == "string"){
copyData[i][j][g] = btoa(data[i][j][g]);
} else {
copyData[i][j][g] = [];
for(var b in data[i][j][g]){
copyData[i][j][g][b] = btoa(data[i][j][g][b]);
}
}
}
}
}
And if u want to console that copyData Object you can use this :
console.debug(JSON.stringify(dd));
The result is this:
var copyData = {
"1":[["aW5wdXQ=","V2FzIHNwaWVsdCBKYWtvYiBnZXJuPw=="],["aW5wdXQgc2FnIGFsdA==",["SmFrb2I=","RXI=","IHNwaWVsdCBuaWNodCBnZXJuIEJhc2tldGJhbGwu"]]],
"2":[["aW5wdXQgYWx0",["TGVuYQ==","U2ll","IGNoYXR0ZXQgZ2Vybi4="]],["aW5wdXQgc2Fn","V2FzIG1hY2h0IExlbmEgbmljaHQgZ2Vybj8="]],
"3":[["aW5wdXQgYWx0",["TWVpa2U=","U2ll","IHNpZWh0IGdlcm4gZmVybi4="]],["aW5wdXQgc2Fn","V2FzIG1hY2h0IE1laWtlIG5pY2h0IGdlcm4/"]],
"4":[["aW5wdXQ=","V2FzIHNwaWVsdCBUaW0gZ2Vybj8="],["aW5wdXQgc2FnIGFsdA==",["VGlt","RXI=","IHNwaWVsdCBuaWNodCBnZXJuIEZ132JhbGwu"]]],
"5":[["aW5wdXQ=","V2FzIHNwaWVsdCBTaW1vbiBnZXJuPw=="],["aW5wdXQgc2Fn","V2FzIHNwaWVsdCBTaW1vbiBuaWNodCBnZXJuPw=="]],
"6":[["aW5wdXQgYWx0",["TWVsYW5pZQ==","U2ll","IHRhbnp0IGdlcm4gSGlwIEhvcC4="]],["aW5wdXQgc2Fn","V2FzIHRhbnp0IE1lbGFuaWUgbmljaHQgZ2Vybj8="]]
};
So everything is okey so far. My question is: Is there any array prototype to do this job easier. I look up to Array.prototype.map() but that uses regular arrays or object with out dimention. Is there any easy way to do this or should i use this way all the time :) Thanks for Advance.
2 Answers 2
I would look for a recursive solution:
function encodeString(obj) {
let type = typeof obj;
if (type === 'object') {
for (let key in obj) {
obj[key] = encodeString(obj[key]);
}
} else if (type === 'string') {
obj = btoa(obj);
}
return obj;
}
This will modify the object in place.
Whilst arguably one shouldn't use for ... in
on array objects, I think the use here is justifiable because it simplifies the code and if any (enumerable) string properties were on the array you'd want them updated too. It used to be the case that for ... in
would also enumerate an array's .length
property but in modern browsers that's no longer the case.
If you want a version that doesn't modify in place:
function encodeString(obj) {
let type = typeof obj;
if (type === 'object') {
let tmp = Array.isArray(obj) ? [] : {};
for (let key in obj) {
tmp[key] = encodeString(obj[key]);
}
return tmp;
} else if (type === 'string') {
return btoa(obj);
}
return obj;
}
but note that this is not a completely safe function - it'll produce "interesting" results if passed objects that aren't plain data because it doesn't deep clone everything.
6 Comments
temp
variable and manipulate & return it. Overriding obj
might not be desired in other case.temp
variable would sometimes be []
and other times be {}
An alternative to @Alnitak's answer, you can use array.reduce
with array.map
recursively.
var data = {
"1":[["input","Was spielt Jakob gern?"],["input sag alt",["Jakob", "Er", " spielt nicht gern Basketball."]]],
"2":[["input alt",["Lena", "Sie", " chattet gern."]],["input sag","Was macht Lena nicht gern?"]],
"3":[["input alt",["Meike", "Sie", " sieht gern fern."]],["input sag","Was macht Meike nicht gern?"]],
"4":[["input","Was spielt Tim gern?"],["input sag alt",["Tim", "Er", " spielt nicht gern Fußball."]]],
"5":[["input","Was spielt Simon gern?"],["input sag","Was spielt Simon nicht gern?"]],
"6":[["input alt",["Melanie", "Sie", " tanzt gern Hip Hop."]],["input sag","Was tanzt Melanie nicht gern?"]]
};
var output = Object.keys(data).reduce(function(p,c){
p[c] = btoaArray(data[c]);
return p;
}, {});
function btoaArray(arr){
return arr.map(function(item){
return Array.isArray(item) ? btoaArray(item) : btoa(item)
})
}
console.log(output)
ES6 version
var data = {
"1":[["input","Was spielt Jakob gern?"],["input sag alt",["Jakob", "Er", " spielt nicht gern Basketball."]]],
"2":[["input alt",["Lena", "Sie", " chattet gern."]],["input sag","Was macht Lena nicht gern?"]],
"3":[["input alt",["Meike", "Sie", " sieht gern fern."]],["input sag","Was macht Meike nicht gern?"]],
"4":[["input","Was spielt Tim gern?"],["input sag alt",["Tim", "Er", " spielt nicht gern Fußball."]]],
"5":[["input","Was spielt Simon gern?"],["input sag","Was spielt Simon nicht gern?"]],
"6":[["input alt",["Melanie", "Sie", " tanzt gern Hip Hop."]],["input sag","Was tanzt Melanie nicht gern?"]]
};
var output = Object.keys(data).reduce((p,c)=>(p[c] = btoaArray(data[c]), p), {});
function btoaArray(arr){
return arr.map((item) => Array.isArray(item) ? btoaArray(item) : btoa(item))
}
console.log(output)
Comments
Explore related questions
See similar questions with these tags.
.length
is not an enumerable property