6

I have this javascript objects:

var arr = [{id:'124',name:'qqq'}, 
           {id:'589',name:'www'}, 
           {id:'45',name:'eee'},
           {id:'567',name:'rrr'}]

I need to rename properties with name in arr1 to title:

var arr = [{id:'124',title:'qqq'}, 
           {id:'589',title:'www'}, 
           {id:'45',title:'eee'},
           {id:'567',title:'rrr'}]

What is elegant way to implement it in Javascript?

0

5 Answers 5

16

Use delete to remove a property from an object. Use obj['newProperty'] to assign newProperty to an object.

var arr = [{id:'124',name:'qqq'}, 
           {id:'589',name:'www'}, 
           {id:'45',name:'eee'},
           {id:'567',name:'rrr'}];
           
arr.forEach( function(data) {
  data['title'] = data['name'];
  delete data['name'];
});

console.log(arr);

You can also use array#map

const arr = [{ id: '124', name: 'qqq' }, { id: '589', name: 'www' }, { id: '45', name: 'eee' }, { id: '567', name: 'rrr' } ],
      result = arr.map(({name, ...rest}) => ({...rest, title: name}));
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

5

Use map :

var arr = [{
    id: '124',
    name: 'qqq'
  },
  {
    id: '589',
    name: 'www'
  },
  {
    id: '45',
    name: 'eee'
  },
  {
    id: '567',
    name: 'rrr'
  }
]

arr = arr.map((e) => {
  return {
    id: e.id,
    title: e.name
  }
});

console.log(arr);

3 Comments

can I use map method in every browser?
@Michael Did you follow my link ?
@Michael yes... ie9+
2
arr.map(e => { return { id: e.id, title: e.name }});

2 Comments

I'd recommen to add some comment to your answer. Without it, it may be not obvious that, for instance, if some object has an extra property (say, "date"), your solution will cause its "deletion" in the array.
@MarceloGondim map does not modify the original array, you either have to reassign arr to the result of this, or you have to assign it to a new variable.
2

it is not possible to change the name of property, but add a new one and delete the old one, i also got the code from somewhere dont remeber the exact source

function changeKey(originalKey, newKey, arr)
{
  var newArr = [];
  for(var i = 0; i < arr.length; i++)
  {
    var obj = arr[i];
    obj[newKey] = obj[originalKey];
    delete(obj[originalKey]);
    newArr.push(obj);
  }
  return newArr;
}

Comments

0

Here's how you delete key and value and assign to new key:

var arr = [{id:'124',name:'qqq'}, 
           {id:'589',name:'www'}, 
           {id:'45',name:'eee'},
           {id:'567',name:'rrr'}]
           
arr.forEach(function(o) {
    Object.defineProperty(o, 'title',
        Object.getOwnPropertyDescriptor(o, 'name'));
    delete o['name'];
});           
console.log(arr);

Comments

Your Answer

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.