-1

I need to change all keys in my Objects which are in the array.

const arr = [
{
"Title" : 'test',
"Format" : "BLA-BLA",
"Start Year" : '2012',
"Stars" : []
},
{
"Title" : 'test2',
"Format" : "BLA-BLA2",
"Start Year" : '2032',
"Stars" : []
}
]

I need to change "Title" to "name", "Format" to "format", "Start year" to "year", "Stars" to "actors"

How can I do this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Anon
  • 307
  • 1
  • 5
  • 17
  • 1
    where does the original data come from? you can use `delete obj[key]` – Daniel A. White Jun 17 '19 at 17:45
  • 1
    Possible duplicate of [Changing the key name in an array of objects?](https://stackoverflow.com/questions/6809659/changing-the-key-name-in-an-array-of-objects) – Heretic Monkey Jun 17 '19 at 17:51
  • You have to try something before asking a question, you should not ask others to write something for you. If you can't get it to work, you have to post what you have tried and why it didn't work – Ruan Mendes Jun 17 '19 at 17:51

3 Answers3

0

Try with map, I hope this helps,

const arr = [
  {
    Title: 'test',
    Format: 'BLA-BLA',
    'Start Year': '2012',
    Stars: []
  },
  {
    Title: 'test2',
    Format: 'BLA-BLA2',
    'Start Year': '2032',
    Stars: []
  }
];


const res = arr.map(obj => {
  const title = obj.Title;
  const format = obj.Format;
  const year = obj['Start Year'];
  const starts = obj.Stars;
  return {
    name: title,
    format,
    year,
    actors: starts
  };
});

console.log(res);
German
  • 557
  • 3
  • 5
0

Just use .map

const arr = [{
    "Title": "test",
    "Format": "BLA-BLA",
    "Start Year": "2012",
    "Stars": []
},
{
    "Title": "test2",
    "Format": "BLA-BLA2",
    "Start Year": "2032",
    "Stars": []
}
];

const result = arr.map((item) => {
return {
    name: item.Title,
    format: item.Format,
    year: item["Start Year"],
    actors: item.Stars
};
});

console.log(result);
Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
-1

You can create a keyMapper and loop through your data and change keys

const arr = [{"Title" : 'test',"Format" : "BLA-BLA","Start Year" : '2012',"Stars" : []},{"Title" : 'test2',"Format" : "BLA-BLA2","Start Year" : '2032',"Stars" : []}]

const keyMapper = {
  "Title" : "name",
  "Format" : "format",
  "Start Year" : "year", 
  "Stars" : "actors"
}

const final = arr.map(val => {
  return Object.entries(val).reduce((op,[key,value]) => {
    op[keyMapper[key]] = value
    return op
  },{})
})

console.log(final)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • Not the downvoter, but this doesn't change the keys, it creates new objects, maybe that's what the downvoter was thinking, even if modifying objects leads to hard to find bugs. If the OP couldn't come up with anything, this is probably too advanced for them. Having said that, I prefer not to write code for OPs that didn't attempt anything – Ruan Mendes Jun 17 '19 at 17:54
  • @JuanMendes thanks for your response mate :) if that is reason for down vote i am happy to have it :) – Code Maniac Jun 17 '19 at 18:04
  • Wow, yet another downvote, some trigger happy people out there. Maybe there's something I can't see – Ruan Mendes Jun 19 '19 at 12:17