Pivot or Transforming JavaScript object

后端 未结 2 1290
忘掉有多难
忘掉有多难 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<result.length;i++){
                    if(result[i][elm[propname]]){
                        result[0][elm[propname]].push(charr);
                        //console.log(result[i][propname]);
                        flag = 1;
                    }
                }
    
                if(flag == 0){
                    chli.push(charr);
                    arr["count"] = checkarr(obj,propname,elm[propname]);        
                    arr[elm[propname]]=chli;
                    result.push(arr);
                }
            });
        }
    
        function checkarr(obj,propname,value){
            var count = 0;
            obj.forEach(function(elm,i) {
                if(elm[propname] == value){
                    count++;
                }
            });
    
            return count;
        }
    
    console.log(result);
    
    0 讨论(0)
  • 2021-01-17 08:28

    I don't know why you want the .count property, when that can be accessed via the array's .length property, but anyway:

    const input = [
      {"name": "Dan",  "city" : "Columbus", "ZIP":"47201"},
      {"name": "Jen",  "city" : "Columbus", "ZIP":"47201"},
      {"name": "Mark", "city" : "Tampa",  "ZIP":"33602"},
    ]
    
    const working = input.reduce((acc, {ZIP, name, city}) => {
      (acc[ZIP] || (acc[ZIP] = [])).push({name, city})
      return acc
    }, {})
    
    const output = Object.keys(working)
      .map(k => ({[k]: working[k], count: working[k].length}))
    
    console.log(output)

    Further reading:

    • Array .reduce()
    • Array .map()
    • Object.keys()
    • Unpacking fields from objects passed as function parameters
    • Computed property names
    0 讨论(0)
提交回复
热议问题