2

I have a some json data that I want to transform into an array of objects that should have the following structure:

var names =[{
   name:"2Anita",
   years:[1916,1917],
   born:[11,20]
},
{
   name:"2Anna",
   years:[1916,1917],
   born:[153,91]
}]

The source data has this structure:

{
    "people": [{
        "key": ["2Anita", "1916"],
        "values": ["11"]
    }, {
        "key": ["2Anita", "1917"],
        "values": ["20"]
    },  {
        "key": ["2Anna", "1916"],
        "values": ["153"]
    }, {
        "key": ["2Anna", "1917"],
        "values": ["91"]
    }]
}

This is what I have achieved so far:

var people = [{
        "key": ["2Anita", "1916"],
        "values": ["11"]
    }, {
        "key": ["2Anita", "1917"],
        "values": ["20"]
    },  {
        "key": ["2Ann", "1920"],
        "values": [".."]
    }, {
        "key": ["2Anna", "1916"],
        "values": ["153"]
    }, {
        "key": ["2Anna", "1917"],
        "values": ["91"]
    }, {
        "key": ["2Ann-Christin", "1916"],
        "values": [".."]
    }, {
        "key": ["2Ann-Christin", "1917"],
        "values": [".."]
    }]

var tempNames = [];
var names = [];

    //Creating array that holds every unique name
    people.forEach(function functionName(v, k) {
        if (tempNames.indexOf(v.key[0]) === -1) {
            tempNames.push(v.key[0])
        }
    });

    //Creating array with objects for each unique name
    tempNames.forEach(function(v, k) {
        names.push({
            name: v,
            years: [],
            born: []
        })
    });

JS Bin:

https://jsbin.com/qofuqatoqo/1/edit?html,js,console

EDIT:

My final solution:

  var grouped = _.groupBy(people, function(num) {
        return num.key[0];
    });

    var j = 0;
    var n = _.each(grouped, function(val) {
        vm.names.push({
            name: val[0].key[0],
            years: [],
            born: []
        })
        for (var i = 0; i < val.length; i++) {
            vm.names[j].years.push(val[i].key[1]);
            vm.names[j].born.push(val[i].values[0]);
            vm.years.push(val[i].key[1]);
        }
        j++;
    });
1
  • Your final solution is good, for all others: _ is object of underscore.js library. Commented Jan 26, 2017 at 8:48

2 Answers 2

1

My solution, uses object as dictionary to store are keys, then transform that dictionary to desired array.

var input={
    "people": [{
        "key": ["2Anita", "1916"],
        "values": ["11"]
    }, {
        "key": ["2Anita", "1917"],
        "values": ["20"]
    },  {
        "key": ["2Anna", "1916"],
        "values": ["153"]
    }, {
        "key": ["2Anna", "1917"],
        "values": ["91"]
    }]
}

var inputArray = input.people;

var dictionary = {}
inputArray.forEach(function(v){
  if(dictionary[v.key[0]]==null)
  {
    dictionary[v.key[0]] = {
      years:[parseInt(v.key[1])],
      born:[ parseInt(v.values[0])]
    }
  } else {
    dictionary[v.key[0]].years.push(parseInt(v.key[1]));
    dictionary[v.key[0]].born.push(parseInt(v.values[0]));
  }
});

var final = [];

for (var key in dictionary)
{
  final.push({
   name: key,
   years:dictionary[key].years,
   born:dictionary[key].born
  });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here's my approach

let data = {
    "people": [{
        "key": ["2Anita", "1916"],
        "values": ["11"]
    }, {
        "key": ["2Anita", "1917"],
        "values": ["20"]
    },  {
        "key": ["2Anna", "1916"],
        "values": ["153"]
    }, {
        "key": ["2Anna", "1917"],
        "values": ["91"]
    }]
}

let auxNames = data.people.reduce((initial, item) => {

  if (!initial.hasOwnProperty(item.key[0])) {
          initial[item.key[0]] = {
      years: [],
      born: []
    }  
  }

  initial[item.key[0]].years.push(item.key[1])
  initial[item.key[0]].born.push(item.values[0])

    return initial
}, {})

const names = []

for (let prop in auxNames) {
    names.push({
    name: prop,
    years: auxNames[prop].years,
    born: auxNames[prop].born
  })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.