0

Efficient Logic to reduce time complexity(n^3) and get all variables

  • getrewarddata is object
  • d1,d2,d3... are variables

//Object has multiple rewards and different month
const getrewarddata = [{
        month: 6,
        year: 2021,
        reward: 6,
    },
    {
        month: 1,
        year: 2021,
        reward: 6,
    },
    {
        month: 3,
        year: 2021,
        reward: 6,
    },
];

//break array in particular month variable
let d3 = getrewarddata.filter((data) => data.month === 3);

let d2 = getrewarddata.filter((data) => data.month === 2);

let d1 = getrewarddata.filter((data) => data.month === 1);

console.log(d2);
console.log(d3);
tirth1620
  • 154
  • 1
  • 8
  • What is the expected result ? Combine them to get what ? – azro Aug 30 '21 at 19:01
  • I wanted array for seprate month like..for month 1, d1 array. – tirth1620 Aug 30 '21 at 19:04
  • It's looks like you're using JavaScript, but you've also tagged this question with `Java`. Which is it to be? – Andy Aug 30 '21 at 19:05
  • You're either writing this code in JavaScript _or_ Java. They are not the same language. Spamming your question with irrelevant tags is not a good approach. – Andy Aug 30 '21 at 19:11
  • thanks Andy for correcting me. I removed it. – tirth1620 Aug 30 '21 at 19:14
  • Your code also works. What output were you expecting? – Andy Aug 30 '21 at 19:14
  • If you're asking how to make that into a function, use the function keyword `function getDataForMonth(rewardData, month) { return rewardData.filter((data) => data.month === month); }`. I mean, that's programming 101, and I don't mean to talk down or anything like that, but you haven't really given us much to go on in the question. – Heretic Monkey Aug 30 '21 at 19:17
  • There is nothing wrong with 2 3 filters running one after the other. What do you think is inefficient here? Is the list too long or are there a lot of such variables? Need more details please. – Mustehssun Iqbal Aug 30 '21 at 19:19
  • Sounds like you want this: https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key/40774906 (I like the reduce method, but there would be multiple different ones) – A_A Aug 30 '21 at 19:19
  • The time complexity is O(3n)=O(n) currently, not O(n^3) – A_A Aug 30 '21 at 19:21

4 Answers4

1

I think you search for a grouping method. And this one works fine: https://stackoverflow.com/a/34890276/11139208

//Object has multiple rewards and different month
const getrewarddata = [{
        month: 6,
        year: 2021,
        reward: 6,
    },
    {
        month: 1,
        year: 2021,
        reward: 6,
    },
    {
        month: 3,
        year: 2021,
        reward: 6,
    },
];

var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};


var groupedByMonth = groupBy(getrewarddata, "month");

console.log(groupedByMonth);
hu-zza
  • 79
  • 1
  • 1
  • 9
0

The only reasonable way to make this easier is to create a helper/wrapper function to skip the code duplicity.

const wrapper = (month) => getrewarddata.filter((data) => data.month === month);

Then just use this helper/wrapper:

let d3 = wrapper(3);
let d2 = wrapper(2);
let d1 = wrapper(1);
Diceros
  • 83
  • 6
0

Your code seems working. Not exactly sure what you want to achieve. You can create a function to call for each variable, to reduce the amount of code.

const getrewarddata = [{
        month: 6,
        year: 2021,
        reward: 6,
    },
    {
        month: 1,
        year: 2021,
        reward: 6,
    },
    {
        month: 3,
        year: 2021,
        reward: 6,
    },
];

function filter(arr, n){
    return arr.filter((data) => data.month === n)
}

let d1 = filter(getrewarddata, 1)
let d3 = filter(getrewarddata, 3)
console.log(d1)
console.log(d3)
J_K
  • 688
  • 5
  • 12
0

This is another take where you have to iterate over the array only once.

let result = getrewarddata.reduce((acc, entry) => {
        if (acc[entry.month] == undefined) { 
            acc[entry.month] = [entry];
        } else {
            acc[entry.month].push(month)
        }
        return acc
    }, {})

This way you get an object like this:

{
    "1": [
        {
            "month": 1,
            "year": 2021,
            "reward": 6
        }
    ],
    "3": [
        {
            "month": 3,
            "year": 2021,
            "reward": 6
        }
    ],
    "6": [
        {
            "month": 6,
            "year": 2021,
            "reward": 6
        }
    ]
}

Which you can query like a regular object

result[1]
=> [{
        "month": 1,
        "year": 2021,
        "reward": 6
    }]
Andreas.Ludwig
  • 372
  • 3
  • 6