[英]Reverse An object in javascript
I want to reverse an object in JavaScript. 我想反转JavaScript中的对象。 For Example:
例如:
Input: obj ={'one': 1, 'two': 2, 'three':3 }
输入:
obj ={'one': 1, 'two': 2, 'three':3 }
output: obj ={'three': 3, 'two': 2, 'one':1 }
输出:
obj ={'three': 3, 'two': 2, 'one':1 }
Is there any method in javascript or lodash? javascript或lodash中有任何方法吗?
here is what you're looking for, 这就是您要寻找的
function dict_reverse(obj) { new_obj= {} rev_obj = Object.keys(obj).reverse(); rev_obj.forEach(function(i) { new_obj[i] = obj[i]; }) return new_obj; } my_dict = {'one': 1, 'two': 2, 'three':3 } rev = dict_reverse(my_dict) console.log(rev)
do you need something like this ? 你需要这样的东西吗?
let obj ={'one': 1, 'two': 2, 'three':3 }; let result = {}, stack = []; for(property in obj){ stack.push({'property' : property, 'value' : obj[property]}) } for(let i=stack.length-1;i>=0;i--){ result[stack[i].property] = stack[i].value; } console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.