Use sub-property to create new multidimensional object [duplicate]












0















This question already has an answer here:




  • How to group an array of objects by key

    11 answers



  • Most efficient method to groupby on a array of objects

    33 answers




I'm receiving from an API objects in following format:



{
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
{
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}


Now I want to rearrange this object to a multidimensonal object (array of objects would be the correct terminus?):



{
'New York': {
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
'Los Angeles': {
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}
}


Is there a proper way to do this? Coming from the PHP world and don't really know how to manage this.



Tried some things, this is what I had in mind, but just getting an empty object back (hung on the first step, create the multidimensional object with birthplace as property, as I cannot use the "push" function (I just don't know why).



var myList = [
{
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
{
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}
];

function rearrange(object) {
var newObj = {};
for(var obj in object) {
if(newObj.hasOwnProperty(obj.birthplace)) {
newObj.push(obj.birthplace)
}

newObj[obj.birthplace].push(obj);
}

return newObj;
}

console.log(rearrange(myList)); // returns: Cannot read proerty 'push' of undefined









share|improve this question













marked as duplicate by Luis felipe De jesus Munoz, Nina Scholz arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 13:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • do you want to get an array of object, grouped by birthplace instead of haveing an single object as grouping result?
    – Nina Scholz
    Nov 20 at 13:21










  • Well, I need to group all the entries by birthplace, so I can output them in a table and count some informations stored in each entry.
    – Matt Backslash
    Nov 20 at 13:24










  • @CalvinNunes that doesn't make any sense
    – charlietfl
    Nov 20 at 13:31










  • Hint: You never created any arrays to push into .... newObj[obj.birthplace] =
    – charlietfl
    Nov 20 at 13:31










  • There are 3 errors in your code : 1/ in javascript, the for ... in loops over keys, not values. And it is discouraged for going through an array, use a classic for (i=0; i < object.length; i++) instead. 2/ Your test with hasOwnProperty is inverted, so it never pass inside. Invert it with ! 3/ newObj is an object, so you can't push on it, you must assign a new empty array to newObj[obj.birthplace]
    – Pierre-Olivier Vares
    Nov 20 at 13:33


















0















This question already has an answer here:




  • How to group an array of objects by key

    11 answers



  • Most efficient method to groupby on a array of objects

    33 answers




I'm receiving from an API objects in following format:



{
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
{
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}


Now I want to rearrange this object to a multidimensonal object (array of objects would be the correct terminus?):



{
'New York': {
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
'Los Angeles': {
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}
}


Is there a proper way to do this? Coming from the PHP world and don't really know how to manage this.



Tried some things, this is what I had in mind, but just getting an empty object back (hung on the first step, create the multidimensional object with birthplace as property, as I cannot use the "push" function (I just don't know why).



var myList = [
{
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
{
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}
];

function rearrange(object) {
var newObj = {};
for(var obj in object) {
if(newObj.hasOwnProperty(obj.birthplace)) {
newObj.push(obj.birthplace)
}

newObj[obj.birthplace].push(obj);
}

return newObj;
}

console.log(rearrange(myList)); // returns: Cannot read proerty 'push' of undefined









share|improve this question













marked as duplicate by Luis felipe De jesus Munoz, Nina Scholz arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 13:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • do you want to get an array of object, grouped by birthplace instead of haveing an single object as grouping result?
    – Nina Scholz
    Nov 20 at 13:21










  • Well, I need to group all the entries by birthplace, so I can output them in a table and count some informations stored in each entry.
    – Matt Backslash
    Nov 20 at 13:24










  • @CalvinNunes that doesn't make any sense
    – charlietfl
    Nov 20 at 13:31










  • Hint: You never created any arrays to push into .... newObj[obj.birthplace] =
    – charlietfl
    Nov 20 at 13:31










  • There are 3 errors in your code : 1/ in javascript, the for ... in loops over keys, not values. And it is discouraged for going through an array, use a classic for (i=0; i < object.length; i++) instead. 2/ Your test with hasOwnProperty is inverted, so it never pass inside. Invert it with ! 3/ newObj is an object, so you can't push on it, you must assign a new empty array to newObj[obj.birthplace]
    – Pierre-Olivier Vares
    Nov 20 at 13:33
















0












0








0








This question already has an answer here:




  • How to group an array of objects by key

    11 answers



  • Most efficient method to groupby on a array of objects

    33 answers




I'm receiving from an API objects in following format:



{
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
{
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}


Now I want to rearrange this object to a multidimensonal object (array of objects would be the correct terminus?):



{
'New York': {
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
'Los Angeles': {
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}
}


Is there a proper way to do this? Coming from the PHP world and don't really know how to manage this.



Tried some things, this is what I had in mind, but just getting an empty object back (hung on the first step, create the multidimensional object with birthplace as property, as I cannot use the "push" function (I just don't know why).



var myList = [
{
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
{
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}
];

function rearrange(object) {
var newObj = {};
for(var obj in object) {
if(newObj.hasOwnProperty(obj.birthplace)) {
newObj.push(obj.birthplace)
}

newObj[obj.birthplace].push(obj);
}

return newObj;
}

console.log(rearrange(myList)); // returns: Cannot read proerty 'push' of undefined









share|improve this question














This question already has an answer here:




  • How to group an array of objects by key

    11 answers



  • Most efficient method to groupby on a array of objects

    33 answers




I'm receiving from an API objects in following format:



{
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
{
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}


Now I want to rearrange this object to a multidimensonal object (array of objects would be the correct terminus?):



{
'New York': {
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
'Los Angeles': {
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}
}


Is there a proper way to do this? Coming from the PHP world and don't really know how to manage this.



Tried some things, this is what I had in mind, but just getting an empty object back (hung on the first step, create the multidimensional object with birthplace as property, as I cannot use the "push" function (I just don't know why).



var myList = [
{
id: 1,
first_name: 'Foo',
last_name: 'Bar',
birthdate: '1990-07-01',
birthplace: 'New York'
},
{
id: 2,
first_name: 'Mary',
last_name: 'Lou',
birthdate: '1984-12-15',
birthplace: 'Los Angeles'
}
];

function rearrange(object) {
var newObj = {};
for(var obj in object) {
if(newObj.hasOwnProperty(obj.birthplace)) {
newObj.push(obj.birthplace)
}

newObj[obj.birthplace].push(obj);
}

return newObj;
}

console.log(rearrange(myList)); // returns: Cannot read proerty 'push' of undefined




This question already has an answer here:




  • How to group an array of objects by key

    11 answers



  • Most efficient method to groupby on a array of objects

    33 answers








javascript arrays object multidimensional-array






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 13:19









Matt Backslash

3061213




3061213




marked as duplicate by Luis felipe De jesus Munoz, Nina Scholz arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 13:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Luis felipe De jesus Munoz, Nina Scholz arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 13:26


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • do you want to get an array of object, grouped by birthplace instead of haveing an single object as grouping result?
    – Nina Scholz
    Nov 20 at 13:21










  • Well, I need to group all the entries by birthplace, so I can output them in a table and count some informations stored in each entry.
    – Matt Backslash
    Nov 20 at 13:24










  • @CalvinNunes that doesn't make any sense
    – charlietfl
    Nov 20 at 13:31










  • Hint: You never created any arrays to push into .... newObj[obj.birthplace] =
    – charlietfl
    Nov 20 at 13:31










  • There are 3 errors in your code : 1/ in javascript, the for ... in loops over keys, not values. And it is discouraged for going through an array, use a classic for (i=0; i < object.length; i++) instead. 2/ Your test with hasOwnProperty is inverted, so it never pass inside. Invert it with ! 3/ newObj is an object, so you can't push on it, you must assign a new empty array to newObj[obj.birthplace]
    – Pierre-Olivier Vares
    Nov 20 at 13:33




















  • do you want to get an array of object, grouped by birthplace instead of haveing an single object as grouping result?
    – Nina Scholz
    Nov 20 at 13:21










  • Well, I need to group all the entries by birthplace, so I can output them in a table and count some informations stored in each entry.
    – Matt Backslash
    Nov 20 at 13:24










  • @CalvinNunes that doesn't make any sense
    – charlietfl
    Nov 20 at 13:31










  • Hint: You never created any arrays to push into .... newObj[obj.birthplace] =
    – charlietfl
    Nov 20 at 13:31










  • There are 3 errors in your code : 1/ in javascript, the for ... in loops over keys, not values. And it is discouraged for going through an array, use a classic for (i=0; i < object.length; i++) instead. 2/ Your test with hasOwnProperty is inverted, so it never pass inside. Invert it with ! 3/ newObj is an object, so you can't push on it, you must assign a new empty array to newObj[obj.birthplace]
    – Pierre-Olivier Vares
    Nov 20 at 13:33


















do you want to get an array of object, grouped by birthplace instead of haveing an single object as grouping result?
– Nina Scholz
Nov 20 at 13:21




do you want to get an array of object, grouped by birthplace instead of haveing an single object as grouping result?
– Nina Scholz
Nov 20 at 13:21












Well, I need to group all the entries by birthplace, so I can output them in a table and count some informations stored in each entry.
– Matt Backslash
Nov 20 at 13:24




Well, I need to group all the entries by birthplace, so I can output them in a table and count some informations stored in each entry.
– Matt Backslash
Nov 20 at 13:24












@CalvinNunes that doesn't make any sense
– charlietfl
Nov 20 at 13:31




@CalvinNunes that doesn't make any sense
– charlietfl
Nov 20 at 13:31












Hint: You never created any arrays to push into .... newObj[obj.birthplace] =
– charlietfl
Nov 20 at 13:31




Hint: You never created any arrays to push into .... newObj[obj.birthplace] =
– charlietfl
Nov 20 at 13:31












There are 3 errors in your code : 1/ in javascript, the for ... in loops over keys, not values. And it is discouraged for going through an array, use a classic for (i=0; i < object.length; i++) instead. 2/ Your test with hasOwnProperty is inverted, so it never pass inside. Invert it with ! 3/ newObj is an object, so you can't push on it, you must assign a new empty array to newObj[obj.birthplace]
– Pierre-Olivier Vares
Nov 20 at 13:33






There are 3 errors in your code : 1/ in javascript, the for ... in loops over keys, not values. And it is discouraged for going through an array, use a classic for (i=0; i < object.length; i++) instead. 2/ Your test with hasOwnProperty is inverted, so it never pass inside. Invert it with ! 3/ newObj is an object, so you can't push on it, you must assign a new empty array to newObj[obj.birthplace]
– Pierre-Olivier Vares
Nov 20 at 13:33



















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno