1

I have an object "member", it has keys and each keys has arrays of objects

var members = {
    'Father' : [
        {
            name: 'John Doe'
        }
    ],
    'Mother' : [
        {
            name: 'Jane Doe'
        }
    ],
    'Uncles': [
        {
            name: 'James Doe'
        },
        {
            name: 'Sam Smith'
        },
        {
            name: 'Mark Smith'
        }
    ],
    'Aunties': [
        {
            name: 'Mary Doe'
        },
        {
            name: 'Mae Smith'
        }
    ]
}

I want to split them into two arrays

var allMembers = [];
var half = Math.ceil(Object.keys(members).length / 2);
var firstHalf = Object.keys(members).splice(0, 4);
var secondHalf = Object.keys(members).splice(4);
allMembers.push(firstHalf)
allMembers.push(secondHalf)

But the result of allMembers is just this:

[["Father", "Mother"], ["Uncles", "Aunties"]]

I want to make it like this result:

0: [
   'Father' : [
        {
            name: 'John Doe'
        }
   ],
   'Mother': [
        {
            name: 'Jane Doe'
        }
   ]
],
1: [
   'Uncles': [
        {
            name: 'James Doe'
        },
        {
            name: 'Sam Smith'
        },
        {
            name: 'Mark Smith'
        }
    ],
    'Aunties': [
        {
            name: 'Mary Doe'
        },
        {
            name: 'Mae Smith'
        }
    ]
]

How can I achieve that?

Here's the fiddle: https://jsfiddle.net/mqk6bswu/

4
  • Your desired result is invalid JavaScript; Arrays have numeric keys; you'd need two objects in an array (e.g. [{ Fathers: [], Mothers: [] }, { Uncles: [], Aunties: [] }]) Commented May 17, 2021 at 13:35
  • yes @HereticMonkey, how can I achieve like that? Commented May 17, 2021 at 13:36
  • Does this answer your question? Javascript split object by key Commented May 17, 2021 at 13:39
  • Not really, but @adiga already answered.. Thanks! Commented May 17, 2021 at 13:43

2 Answers 2

3

Instead of getting keys, you could get entries of the object and split them into 2 parts. Use Object.fromEntries() on both arrays to get an array with two objects

const members={Father:[{name:"John Doe"}],Mother:[{name:"Jane Doe"}],Uncles:[{name:"James Doe"},{name:"Sam Smith"},{name:"Mark Smith"}],Aunties:[{name:"Mary Doe"},{name:"Mae Smith"}]},
      entries = Object.entries(members),
      half = Math.ceil(entries.length / 2),
      firstHalf = entries.splice(0, half),
      secondHalf = entries, // remaining
      allMembers = [firstHalf, secondHalf].map(Object.fromEntries);

console.log(allMembers)

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

Comments

1

The most straighforward way to achive [{ Fathers: [], Mothers: [] }, { Uncles: [], Aunties: [] }] is to map it manually. For example;

const result = [{Fathers: members.Fathers, Mothers: members.Mothers }, { Uncles: members.Uncles, Aunties: members.Aunties}];

A more advanced solution will be to use Object.entries and map the data dynamically.

1 Comment

Thanks.. I like @adiga's answer, it's more dynamic

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.