0

How to change just value in object key?

Ex: I would like to update keyvalue list_one to anything value.

var ar = [
  {
    "list_one": [
      { id:1,product: 'Fortenza'}
    ]
  },
  {
    "list_two": [
      { id:4,product:'SETLIQUI'}
    ]
  }
]

My code:

var setTitleProduct = function(key="list_one",index=0) {
  Object.keys(ar[index][key])[0] = 'new value'; //I know this doesnt work
  console.log(ar[index][key]);
}
setTitleProduct("list_one",0)

Like this. https://i.stack.imgur.com/2U9gd.png

Herick
  • 217
  • 1
  • 3
  • 13

1 Answers1

0

var ar = [
  {
    "list_one": [
      { id:1,product: 'Fortenza'}
    ]
  },
  {
    "list_two": [
      { id:4,product:'SETLIQUI'}
    ]
  }
]

ar[0]['someValue'] = ar[0]['list_one'];
delete ar[0]['list_one'];

console.log(ar);
DCR
  • 14,737
  • 12
  • 52
  • 115