I have an object as per below:
{
items: [
{
id: '6978119a-481e-4ff5-bd02-2b51d2aed804',
name: 'Car',
items: [],
},
{
id: '9cc8fe29-1106-4f3e-958d-250ca74cc1e8',
name: 'Toolbox',
items: [
{
id: '1764e295-4ee7-4455-80b4-ea011e111785',
name: 'Spanner',
items: [],
},
{
id: '3a2c1c78-9c97-457e-bfdd-ea13ced94406',
name: 'Hammer',
items: [],
},
],
},
{
id: 'aa41d735-ebb7-48dd-a5b0-dc6806c4ee95',
name: 'Shed',
items: [
{
id: '0a81991c-51e7-4cea-b1cf-a63ea7200744',
name: 'Axe',
items: [],
},
],
},
],
};
I am trying to figure out two things:
- Get an array of objects, where the key is 'name' (e.g. below)
var array = [{name: 'Car'}, {name: 'Toolbox'}, {name: 'Spanner'}, {name: 'Hammer'}, {name: 'Shed'}, {name: 'Axe'}]
- Get an array of objects, where the key is 'name' but has no items below it (essentially childless) (e.g. below)
var array = [{name: 'Car'}, {name: 'Spanner'}, {name: 'Hammer'}, {name: 'Axe'}]
I attempted putting something together that iterates to a list and pushes to an array but after the 3rd nested loop, I knew I was doing something wrong.
I've also explored Object.entries but it only gets the top layer.