2

I have data structure like below:

a : { active: true, key: "first", page: 1 },
b : { active: true, key: "second", page: 1 },
c : { active: true, key: "third", page: 2 },
d : { active: true , key: "fourth", page: 2 },
e : { active: false , key: "fifth", page: 3 },

From this data, first I need to find data with active status true then I need to create a new array containing sub array with same page number. I need something like this.

[
    ["first", "second"],
    ["third", "fourth"],
    ["fifth"]
]

What I have tried is:

let myArray = Object.entries(dashboardKeyConfiguration).filter(x => x[1]["active"]);

var groups = [];
        for (var i = 0; i < myArray.length; i++) {
            var groupName = myArray[i][1]["page"];
            if (!groups[groupName]) {
                groups[groupName] = [];
            }
            groups[groupName].push(myArray[i][1]["key"]);
        }
        myArray = [];
        console.log(groups);

It is inserting empty as first item like this:

(3) [empty, Array(2), Array(2)]
3
  • 1
    1) You're filtering out items that are not active, so why should "fifth" be included? 2) When I run the code, I see [null,["first","second"],["third","fourth"]]. Most likely you just need to subtract 1 from groupName (because array indexes start at zero). Commented Mar 13, 2019 at 4:14
  • Why you need "fifth" in result. Its acitve is false Commented Mar 13, 2019 at 4:18
  • Sorry, I dont need fifth. i need only 4 based on above Commented Mar 13, 2019 at 4:19

3 Answers 3

2

What we can to is define the pageLength max number of pages we can have. Then we can loop from i to pageLength and search the object for the page and push it in the temp array. Once loop completes for a specific page push the temp array in the main array

const obj = {
        a : { active: true, key: "first", page: 1 },
        b : { active: true, key: "second", page: 1 },
        c : { active: true, key: "third", page: 2 },
        d : { active: true , key: "fourth", page: 2 },
        e : { active: false , key: "fifth", page: 3 },
        }
        let pageLength = 3;
        let array = [] ;
        
        for(let i = 1;i<=pageLength;i++){  
            temp = [];   
            for (var key in obj) {
                if (obj.hasOwnProperty(key)) {
                    let value = obj[key].key;
                    let page = obj[key].page;
                    if(page == i){    
                        temp.push(value);
                    } 
                }
            }
            array.push(temp) 
        }
        
        console.log(array)

It is just a raw logic. Keep in mind that the complexity for this can might be high and you could do it in a better way.

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

1 Comment

if you want to check for status you can also do this let active = obj[key].active if(page == i && active==true)
1

This is because page starts from 1 Just subtract 1 from groupName

let obj = { a : { active: true, key: "first", page: 1 }, b : { active: true, key: "second", page: 1 }, c : { active: false, key: "third", page: 2 }, d : { active: false , key: "fourth", page: 2 }, e : { active: true , key: "fifth", page: 3 }} 


let myArray = Object.entries(obj).filter(x => x[1]["active"]);
var groups = [];
        for (var i = 0; i < myArray.length; i++) {
            var groupName = myArray[i][1]["page"]-1;
            if (!groups[groupName]) {
                groups[groupName] = [];
            }
            groups[groupName].push(myArray[i][1]["key"]);
        }
groups = groups.filter(x => x !== undefined)
console.log(groups)

8 Comments

let obj = { a : { active: true, key: "first", page: 1 }, b : { active: true, key: "second", page: 1 }, c : { active: false, key: "third", page: 2 }, d : { active: false , key: "fourth", page: 2 }, e : { active: true , key: "fifth", page: 3 }} when my data is like this it gives empty again'
That is because this time page:2 is excluded
You can filter() undefined in the end. I have edited
i still have empty in the middle
Its giving [ [ "first", "second" ], [ "fifth" ] ] in my console. Which browser you are using?
|
1
Object.values(Object.keys(obj).reduce((acc,key)=>{
    if(obj[key].active) {if(acc[obj[key].page]) {acc[obj[key].page].push(obj[key].key)} else {acc[obj[key].page] = [obj[key].key]}}
        return acc
},{}))

We can reduce the object entries where the "active key is true" to an object

Object.keys(obj).reduce((acc,key)=>{
     if(obj[key].active) {if(acc[obj[key].page]) {acc[obj[key].page].push(obj[key].key)} else {acc[obj[key].page] = [obj[key].key]}}
        return acc
},{})

output:
{
    1:["first","second"],
    3:["fifth"]
}

We can take the values from this object

[["first","second"],["fifth"]]

For more info https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

1 Comment

Please provide some description along with code so that other people can easily understand your answer

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.