0

I don't think what I want is anything special, I just cant get my head round how to do it. I have an array of objects -

{   
    "year": 2016,
    "some stuff": "bla0",
    "other stuff": 20
},
    "year": 2017,
    "some stuff": "bla1",
    "other stuff": 21
},
    "year": 2016,
    "some stuff": "bla2",
    "other stuff": 22
}

I want to create the following array of objects from the above.

{
    "2016": [
        {
            "year": 2016,
            "some stuff": "bla0",
            "other stuff": 20
        },
        {
            "year": 2016,
            "some stuff": "bla2",
            "other stuff": 22
        }
    ],
    "2017": [
        {
            "year": 2017,
            "some stuff": "bla1",
            "other stuff": 21
        }
    ]
}

My current WIP code is on https://codepen.io/sharperwebdev/pen/aqQQZy/?editors=1011 pertinent JS lines start at 167. Any help with getting my thinking straight on this would be appreciated.

1
  • Probably create a new object, then use array forEach to iterate over each item in the array and check the year-- then see if that year exists as a property in your new object yet. If not, create an property for the year, assigned an array as a value. Then, push the current object in your forEach to the array at that property in the new object. Also, incidentally, it is sort of frowned upon to link out to a whole project. Read minimal reproducible example. Commented Mar 1, 2018 at 15:31

5 Answers 5

1

Use the function reduce

var array = [{    "year": 2016,    "some stuff": "bla0",    "other stuff": 20  },  {    "year": 2017,    "some stuff": "bla1",    "other stuff": 21  },  {    "year": 2016,    "some stuff": "bla2",    "other stuff": 22  }]

var result = array.reduce((a, c) => {
  if (a[c.year]) a[c.year].push(c);
  else a[c.year] = [c];
  return a;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! thanks Ele. This is exactly what I was looking for. The others were good also which is why I've up voted the correctly working solutions.
1

You want to group your array of objects by the year? You can do something like:

const data = [{   
  "year": 2016,
  "some stuff": "bla0",
  "other stuff": 20
}, {
  "year": 2017,
  "some stuff": "bla1",
  "other stuff": 21
}, {
  "year": 2016,
  "some stuff": "bla2",
  "other stuff": 22
}];

function groupBy(data, key) {
  return data.reduce((result, item) => {
    result[item[key]] = result[item[key]] || [];
    result[item[key]].push(item);
    return result;
  }, {});
}

groupBy(data, 'year');

jsbin demo: http://jsbin.com/jifozivuve/edit?js,console

Comments

1

var objArray=[{   
    "year": 2016,
    "some stuff": "bla0",
    "other stuff": 20
},{
    "year": 2017,
    "some stuff": "bla1",
    "other stuff": 21
},{
    "year": 2016,
    "some stuff": "bla2",
    "other stuff": 22
}];
var newObjArray={};

objArray.forEach(function(item){
var year=item.year;
  if(!newObjArray.hasOwnProperty(year)){
    newObjArray[year]=[item];
  }else{
    newObjArray[year].push(item);
  }
});
console.log(newObjArray);

Comments

1

You can loop the existing array using forEach and add year as akey to a new object.

var oldArr = [{
  "year": 2016,
  "some stuff": "bla0",
  "other stuff": 20
}, {
  "year": 2017,
  "some stuff": "bla1",
  "other stuff": 21
}, {
  "year": 2016,
  "some stuff": "bla2",
  "other stuff": 22
}]

var newObj = {};
oldArr.forEach(function(item) {
  if (newObj.hasOwnProperty(item.year)) {
    newObj[item.year].push(item)
  } else {
    newObj[item.year] = [];
    newObj[item.year].push(item)
  }
});
console.log(newObj)
I want to create the following array of objects from the above.

Comments

1

To achieve expected result, use below option of using map

var x = [{   
    "year": 2016,
    "some stuff": "bla0",
    "other stuff": 20
},{
    "year": 2017,
    "some stuff": "bla1",
    "other stuff": 21
},{
    "year": 2016,
    "some stuff": "bla2",
    "other stuff": 22
}]

var y =[]
x.map(function(e){
  var temp ={}
  temp[e.year] = e
  y.push(temp)
})

console.log(y)

https://codepen.io/nagasai/pen/yvZdQY

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.