Pivot or Transforming JavaScript object

后端 未结 2 1291
忘掉有多难
忘掉有多难 2021-01-17 07:51

I have the following JavaScript object. I need to generate a new object from the given object. What is the approach I should take in JavaScript?

[
 {\"name\"         


        
2条回答
  •  遥遥无期
    2021-01-17 08:17

    The below code will work for your requirement. The final result is stored in the variable result which holds the array object.

        var source = [{
                "name": "Dan",
                "city": "Columbus",
                "ZIP": "47201"
            },
            {
                "name": "Mark",
                "city": "Tampa",
                "ZIP": "33602"
            },
            {
                "name": "Jen",
                "city": "Columbus",
                "ZIP": "47201"
            }
        ];
    
        var result = [];
        finalarr('ZIP');
    
        function finalarr(propname) {
            var obj = JSON.parse(JSON.stringify(source));
    
            obj.forEach(function(elm,i) {
                var arr = {};var chli=[];var charr={};
                var flag = 0;
    
                for (var prop in elm) {
                    if(prop != propname){
                        charr[prop]=elm[prop];
                    }
                }
    
                for(var i=0;i

提交回复
热议问题