0

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.

asked Jan 25, 2017 at 9:32
10
  • @Rajesh actually I think this is one of those cases where it might be appropriate - it would automatically convert any named properties that might exist on an array as well as the numeric indices. Commented Jan 25, 2017 at 9:38
  • I have to ask - why on earth do you want to do this? Commented Jan 25, 2017 at 9:39
  • Hello @Rajesh thx for reply. I never say that is the best or good way to do this. I just say that this is my way :) Commented Jan 25, 2017 at 9:39
  • Hello @Alnitak, I need it because users can't see the direct answer's to questions :) Commented Jan 25, 2017 at 9:40
  • 2
    @Rajesh in modern browsers .length is not an enumerable property Commented Jan 25, 2017 at 9:42

2 Answers 2

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.

answered Jan 25, 2017 at 10:03

6 Comments

I believe this one function can't convert all my data? can u write try to convert that data variable which i wrote on the top of this page.
@HalilÇakar I'll test again, but it appeared to work when I tried it
Oww well i just try it out and it WORKS PERFECT. Thank you @Alnitak
Since this is a very generic code, I'd suggest using a temp variable and manipulate & return it. Overriding obj might not be desired in other case.
@Rajesh that would in general be preferred, but it would complicate the code somewhat because the initial value of the temp variable would sometimes be [] and other times be {}
|
1

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)

answered Jan 25, 2017 at 10:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.