最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to rename key of object in an array - Stack Overflow

programmeradmin4浏览0评论

How to replace array element value with another

i have array like this, without using jquery

 this.products = [
      { 
        text: 'prod1', 
        value: 1 
      },
      { 
        text: 'prod2', 
        value: 2 
      },
      { 
        text: 'prod3', 
        value: 3 
      }
 ];

i want to replace 'text' to 'label'

How to replace array element value with another

i have array like this, without using jquery

 this.products = [
      { 
        text: 'prod1', 
        value: 1 
      },
      { 
        text: 'prod2', 
        value: 2 
      },
      { 
        text: 'prod3', 
        value: 3 
      }
 ];

i want to replace 'text' to 'label'

Share Improve this question edited Dec 4, 2017 at 13:02 SALEH 1,5621 gold badge14 silver badges22 bronze badges asked Dec 4, 2017 at 11:55 SrinuSrinu 1512 gold badges2 silver badges11 bronze badges 4
  • 3 products.map(prod => { return { label: prod.text, value: prod.value }; }) – haim770 Commented Dec 4, 2017 at 11:58
  • 3 Possible duplicate of JavaScript: Object Rename Key – Rudresha Parameshappa Commented Dec 4, 2017 at 12:02
  • 2 Try this var result = products.map(({text, value}) => ({label: text, value})); – Hassan Imam Commented Dec 4, 2017 at 12:16
  • 2 thanks @HassanImam working perfect – Srinu Commented Dec 4, 2017 at 12:27
Add a comment  | 

4 Answers 4

Reset to default 13

How about this?

var products = [{
    text: 'prod1',
    value: 1
  },
  {
    text: 'prod2',
    value: 2
  }, {
    text: 'prod3',
    value: 3
  }
];

products.forEach(function(obj) {
  obj.label = obj.text;
  delete obj.text;
});
console.log(products);

using ES6:

const updatedProducts = products.map(({text: label, value})=>({value, label}));
There are many ways using map in ES6



    var products = [
          { 
            text: 'prod1', 
            value: 1 
          },
          { 
            text: 'prod2', 
            value: 2 
          },
          { 
            text: 'prod3', 
            value: 3 
          }
     ];

    const newProducts = products.map(({text: label, value})=>({value, label}));
    console.log(newProducts );

    console.log("===================Another  Method====================")


    products.map((el)=>{
    el.label = el.text
    delete el.text
    })

    console.log(products);

For people like me who are looking for an answer that does not mutate the original objects (which will cause errors in React) but instead want to return a new array full of new objects with only one specific key renamed in each object I have create the following function.

export const renameKey = (arr, oldKey, newKey) => {
  let newArray = [];

  arr.forEach((obj) => {
    let newObj = {};
    const keys = Object.keys(obj);
    keys.forEach((key) => {
      if (key === oldKey) {
        Object.assign(newObj,{ [newKey]: obj[oldKey] });
      } else {
        Object.assign(newObj,{ [key]: obj[key] });
      }
    });
    newArray.push(newObj);
  });

  return newArray;
};
发布评论

评论列表(0)

  1. 暂无评论