Home > Back-end >  JS convert obj to str
JS convert obj to str

Time:07-25

I'm creating code to convert a string to a dictionary and an array, and to convert an array and a dictionary to a string.

String to array works, String to Obj (dictionary) works, array to text works, but obj to string (text) doesn't. I'm stuck and I don't know how to fix it. I tested the code but the results were not good.

function strToArray(string) {
  let array = [];
  for (var element of string) {
    array.push(element);
  }
  return array;
};

function strToObj(string) {
  let dict = {};
  for (let i = 0; i < string.length; i  ) {
    let letter = string[i];
    if (dict[letter]) {
      dict[letter].push(i);
    } else {
      dict[letter] = [i];
    }
  }
  return dict;
};

function arrayToString(array) {
  let text = "";
  for (var element of array) {
    text = text   element;
  }
  return text;
};

function objToString(obj) {
  let text = "";
  //for(i = 0; i < obj.length; i  ){
  //text = text   arrayToString(obj[i]);
  //}
  for (var element in obj) {
    let value = obj[element];

  }
  return text;
};


const ArrayHello = strToArray('hello');
const ObjHello = strToObj('hello');
const HelloArray = arrayToString(ArrayHello);
const HelloObj = objToString(ObjHello);

console.log(ArrayHello);
console.log(ObjHello);
console.log(HelloArray);
console.log(HelloObj);

CodePudding user response:

Something like this could be done for the objToString

function objToString(obj) {
  let text = "";
  let result = [];
  Object.entries(obj).forEach((entry) => {
    entry[1].forEach((ind) => {
      result[ind] = entry[0];
    });
  });
  text = result.join("");
  return text;
}

CodePudding user response:

You can use array to create the string and can reuse arrayToString() method and can get the answer easily like following:

function strToArray(string) {
  let array = [];
  for (var element of string) {
    array.push(element);
  }
  return array;
};

function strToObj(string) {
  let dict = {};
  for (let i = 0; i < string.length; i  ) {
    let letter = string[i];
    if (dict[letter]) {
      dict[letter].push(i);
    } else {
      dict[letter] = [i];
    }
  }
  return dict;
};

function arrayToString(array) {
  let text = "";
  for (var element of array) {
    text = text   element;
  }
  return text;
};

function objToString(obj) {
  let text = [];
  
  for (let [key, index] of Object.entries(obj)) {
    for (let i of index) {
      text[i] = key;
    }
  }
  
  return arrayToString(text);
};


const ArrayHello = strToArray('hello');
const ObjHello = strToObj('hello');
const HelloArray = arrayToString(ArrayHello);
const HelloObj = objToString(ObjHello);

console.log(ArrayHello);
console.log(ObjHello);
console.log(HelloArray);
console.log(HelloObj);

UPDATE:

You can also use Array.prototype.join method to achieve arrayToString functionality easily [MDN LINK]

const array = ['h', 'e', 'l', 'l', 'o'];

const arrayToString = (arr, delimiter = '') => arr.join(delimiter);

console.log(arrayToString(array));

CodePudding user response:

 function strToArray(string){
    let array = [];
    for(var element of string){
        array.push(element);
    }
    return array;
};

function strToObj(string){
    let dict = {};
    for(let i = 0; i < string.length; i  ){
        let letter = string[i];
            dict["key"   i] = letter;
    }
    return dict;
};

function arrayToString(array){
    let text = "";
    for(var element of array){
        text = text element;
    }
    return text;
};

function objToString(obj){
    let text;
    let arry = [];
    for(var element in obj){
        let value = obj[element];
        arry.push(value);
    }
    return text = arry.join('');
};


const ArrayHello = strToArray('hello');
const ObjHello = strToObj('hello');
const HelloArray = arrayToString(ArrayHello);
const HelloObj = objToString(ObjHello);

console.log(ArrayHello);
console.log(ObjHello);
console.log(HelloArray);
console.log(HelloObj);
I hope it might be a solution. I think you need to make changes in your (strToObj and objToString) functions.

CodePudding user response:

function strToArray(string) {
  let array = [];
  for (var element of string) {
    array.push(element);
  }
  return array;
};

function strToObj(string) {
  let dict = {};
  for (let i = 0; i < string.length; i  ) {
    let letter = string[i];
    if (dict[letter]) {
      dict[letter].push(i);
    } else {
      dict[letter] = [i];
    }
  }
  return dict;
};

function arrayToString(array) {
  let text = "";
  for (var element of array) {
    text = text   element;
  }
  return text;
};

function objToString(obj) {
  let text = "";
  
  for (var element in obj) {
   let value = obj[element];
   text =value " ";// in Place of " " you can use any seprator
  }
  return text;
};


const ArrayHello = strToArray('hello');
const ObjHello = strToObj('hello');
const HelloArray = arrayToString(ArrayHello);
const HelloObj = objToString(ObjHello);

console.log(ArrayHello);
console.log(ObjHello);
console.log(HelloArray);
console.log(HelloObj);

  • Related