简体   繁体   English

JavaScript:用数组替换对象键

[英]JavaScript: replacing object keys with an array

I'm new to JS and experimenting the things. 我是JS的新手,正在尝试这些东西。 I have following object: 我有以下对象:

  var data = {'name': 'john', 'old': 18, 'place': 'USA'}

How can I switch the keys of this object with the following array? 如何使用以下数组切换此对象的键?

 var array = ['First Name', 'age', 'country']

To look following: 看下面:

{'First Name': 'john', 'age': 18, 'country': 'USA'}

The only way to rename a key of an object is to assign the value of the old key to the new key and then delete the old key 重命名对象键的唯一方法是将旧键的值分配给新键,然后删除旧键

Object[ new_key ] = Object[ old_key ];
delete Object[ old_key ];

Another way is to create a completely new object (with the new keys), assign the values to the new keys and then delete the old object. 另一种方法是创建一个全新的对象(使用新键),将值分配给新键,然后删除旧对象。

You can use Object.assign() , Object.entries() , .map() , spread element and computed properties to assign the property name to a new object having value to to current index of property, value within iteration, set identifier for original object to result of Object.assign() call 您可以使用Object.assign()Object.entries() .map() ,spread元素和计算出的属性来将属性名称分配给一个新对象,该对象具有与当前属性索引相同的值,迭代内的值,为原始对象到Object.assign()调用的结果

 let array = ['First Name', 'age', 'country'] let data = {'name': 'john', 'old': 18, 'place': 'USA'} data = Object.assign({}, ...Object.entries(data) .map(([, prop], index) => ({[array[index]]: prop}))); console.log(data); 

 var array = ['First Name', 'age', 'country']; var data = {'name': 'john', 'old': 18, 'place': 'USA'}; var keys = Object.keys(data); var newData = {}; for (var a in array) { //using new obj newData[array[a]] = data[keys[a]]; //editing same obj data[array[a]] = data[keys[a]]; delete data[keys[a]]; } console.log(data); console.log(newData); 

 var array = ['First Name', 'age', 'country']; var list = [ { 'name': 'john 1', 'old': 18, 'place': 'USA' }, { 'name': 'john 2', 'old': 19, 'place': 'USB' }, { 'name': 'john 3', 'old': 20, 'place': 'USC' }, ]; var newList = []; for (var item in list) { var newData = {}; for (var a in array) { newData[array[a]] = list[item][Object.keys(list[item])[a]]; } newList.push(newData); } console.log(newList); 

You could use an object with the replacement keys and iterate it for changing the keys. 您可以将对象与替换键一起使用,并对其进行迭代以更改键。

 var data = { name: 'john', old: 18, place: 'USA' }, newKeys = { name: 'First Name', old: 'age', place: 'country' }; Object.keys(newKeys).forEach(function (k) { data[newKeys[k]] = data[k]; delete data[k]; }); console.log(data); 

Rather than switching the object keys; 而不是switching对象键; which cannot be done and you'd have to delete keys and add the new one, you could simply create a new object with the desired keys: 这是无法完成的,您必须删除密钥并添加新密钥,您只需使用所需密钥创建一个新对象:

var data2 = {};
data2['First Name'] = data.name;
data2.age = data.old;
data2country = data.place;
var data = {'name': 'john', 'old': 18, 'place': 'USA'}
var ary = ['First Name', 'age', 'country']

// create key-value pairs array 
var obj_entries = Object.entries(data)

var new_data =ary.reduce((acc, value, idx)=>{
                            acc[value]=obj_entries[idx][1];
                            return acc;
                             }, {})
console.log(new_data)

Maybe a functional approach 也许是一种实用的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM