1

I have a multi dimensional object array:

var products = [  
{ id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.2, selected: false }}, 
{ id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true }} ];

How can I find list of selected groups (group with selected flag set to true) in a single dimensional array using ES6.

Expected Result:

[1.1, 2.2]
0

6 Answers 6

2

You could use reduce method and inside one more forEach loop to get group objects where selected is true.

var products = [  
{ id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.2, selected: false }]}, 
{ id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true }]} ];

const result = products.reduce((r, {groups}) => {
  groups.forEach(e => e.selected && r.push(e.id));
  return r;
}, [])
	
console.log(result)

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

Comments

2

A possible alternative to all the other answers.

Flatten the array, then extract the required info.

const products = [
    { id: 1, groups: [{ id: 1.1, selected: true }, { id: 1.2, selected: false }] },
    { id: 2, groups: [{ id: 2.1, selected: false }, { id: 2.2, selected: true }] }];

const allIds = [].concat(...products.map(p => p.groups)).filter(g => g.selected).map(x => x.id)

console.log(allIds)

Comments

1

You can use reduce and filter like below, this will give you all ids that have selected: true. Note that I changed id: 1.2 to be true to demonstrate the ability of getting multiple true results.

const products = [
  { id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.2, selected: true } ]},
  { id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true } ]} ];

const res = products.reduce((a, {groups}) => 
  a.concat(...groups.filter(({selected}) => selected).map(({id}) => id))
, []);

console.log(res);

Comments

1

var products = [  
{ id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.2, selected: false }]}, 
{ id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true } ]}];

var desiredResult = products.map(product => {
  return product.groups.filter(group => group.selected).map(item => item.id).toString();
});
console.log(desiredResult);

2 Comments

find won't work if you have multiple selected: true in an array
@developer updated answer for multi selected, used filter.
0

you can make use of map,filter and concat function and get result

var products = [  
{ id: 1, groups: [ { id: 1.1, selected: true }, { id: 1.3, selected: true },{ id: 1.2, selected: false }]}, 
{ id: 2, groups: [ { id: 2.1, selected: false }, { id: 2.2, selected: true }]} ];

const selectgroups =  [].concat(...products.map(x => x.groups.filter(g=> g.selected)));
const selectedGroupIds = selectgroups.map(g=> g.id);

console.log(selectedGroupIds);

code which provide list of all selected groups

Working Demo

1 Comment

and what is reason of - 1?... i answered late but i was help op from begining and based on input provided ...and deleted few answer as its downvoted based on old information
0

I see a completely different approach: using specialized data structures to model the data the way you want to work with it:

// Store product id mapped to a set of
// group ids
const productGroupMap = new Map ( [
   [ 1, new Set ( [ 1.1, 1.2 ] ) ],
   [ 2, new Set ( [ 2.1, 2.2 ] ) ]
] )

// Store group ids to their data
const groupMap = new Map ( [
   [ 1.1, { title: "Title 1.1" } ],
   [ 1.2, { title: "Title 1.2" } ],
   [ 2.1, { title: "Title 2.1" } ],
   [ 2.2, { title: "Title 2.2" } ]
] )

// Somewhere in your application, you would declare
// a set of selected group ids, and you would add ids
// when the user selects a given group
const selectedGroups = new Set ()

selectedGroups.add ( 1.1 )
selectedGroups.add ( 2.2 )

// Finally, when you want these selected groups, you just need to
// map selected group ids to their group data getting it from the
// groupMap
const groups = [ ...selectedGroups ].map ( id => groupMap.get ( id ) )

console.log ( groups )

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.