0

How to write the method which at the output will convert the key where there is a character "_" into a space. At the same time, we cannot bind to shortcut keys, but to make the method universal, that would itself find and transform.

const arr = [{
        "name": "BMW",
        "price": "55 000",
        "color": "red",
        "constructor_man": "Billy%Zekun" //should become "constructor man"
    }, {
        "name": "MERSEDEC",
        "price": "63 000",
        "color": "blue",
        "constructor_man": "Jon%Adams" //should become "constructor man"
    }, {
        "name_car": "Lada", //should become "name car"
        "price": "93 000",
        "color": "blue",
        "constructor_man": "Bar John", //should become "constructor man"
        "door": "3"
    }, {
        "name": "TOYOTA",
        "price": "48 000",
        "color": "blue",
        "constructor_man": "Jon Hubert", //should become "constructor man"
        "door": "3",
        "max_people": "7" //should become "max people"
    }
];
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
Gregori Roberts
  • 309
  • 3
  • 10

3 Answers3

0

You an loop through the array using for...of. Then loop through each object using for...in. If the key includes _, replace with ' '. delete the key with _ :

const arr = [{"name":"BMW","price":"55 000","color":"red","constructor_man":"Billy%Zekun"},{"name":"MERSEDEC","price":"63 000","color":"blue","constructor_man":"Jon%Adams"},{"name_car":"Lada","price":"93 000","color":"blue","constructor_man":"Bar John","door":"3"},{"name":"TOYOTA","price":"48 000","color":"blue","constructor_man":"Jon Hubert","door":"3","max_people":"7"}];

for (let item of arr) {
  for (let key in item) {
    if (key.includes('_')) {
      item[key.replace(/_/g, ' ')] = item[key];
      delete item[key]
    }
  }
}

console.log(arr)
adiga
  • 34,372
  • 9
  • 61
  • 83
  • Thank you, but the essence of the task is to do this task through the method – Gregori Roberts Apr 11 '19 at 11:43
  • @LoveGIAS I don't understand what you're asking. Write a function with one parameter and put this `for` loop code inside it and call the function with your object? – adiga Apr 11 '19 at 11:56
0

You may use following approach:

const arr = [{
  "name": "BMW",
  "price": "55 000",
  "color": "red",
  "constructor_man": "Billy%Zekun" //should become "constructor man"
 }, {
  "name": "MERSEDEC",
  "price": "63 000",
  "color": "blue",
  "constructor_man": "Jon%Adams" //should become "constructor man"
 }, {
  "name_car": "Lada", //should become "name car"
  "price": "93 000",
  "color": "blue",
  "constructor_man": "Bar John", //should become "constructor man"
  "door": "3"
 }, {
  "name": "TOYOTA",
  "price": "48 000",
  "color": "blue",
  "constructor_man": "Jon Hubert", //should become "constructor man"
  "door": "3",
  "max_people": "7" //should become "max people"
 }
];
const underscoreReplacer = src => src.map(item => Object.entries(item).reduce((obj,keyValue) => {obj[keyValue[0].replace('_',' ')]=keyValue[1];return obj},{}));
console.log(underscoreReplacer(arr));
.as-console-wrapper {
  max-height:100% !important;
  top: 0;
}
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
0
var underScoreReplacer = function(myArray){
    var temp = [];
    myArray.forEach(object => {
       var tempObj = {};
       Object.keys(object).forEach(key => {
            tempObj[key.replace("_", "#")] = object[key];
       });
       temp.push(tempObj);
    });
    return temp;
}

var myArray = [
   {
    name:"BMW",
    price:"55 000", 
    color:"red",
    constructor_man:"Billy%Zekun" //should become "constructor man"
    }
];

myArray = underScoreReplacer(myArray);
console.log(myArray);
Dblaze47
  • 868
  • 5
  • 17