How do I push an array inside an object array

this is driving me nuts. how can I get the numbers in the object inside the date array.

CODE LINK : ’ data manipulation - Replit

const data = [
{"draw_date":"2022-03-16T00:00:00.000","winning_numbers":"34 42 48 51 56 59","bonus":"6"}
]

let groupNumber = {}
const keys = Array.from({length: 6}, (_,y) => (y ))
keys.map(key => groupNumber[key] = [])

data.map( d => {
  // we get the numbers and days
  let nums = d['winning_numbers']
  let days  = d['draw_date']
  getNumbers(days, nums)
})

function getNumbers(day, nums){
  // go to all the keys and push the day and the array to that key if is in the ran
  let fecha = {}
  nums.split(' ').map((x, i ) => {
    if(!fecha[day]){
      fecha[day] = []
    } 
    if(!groupNumber[x[0]][0]) groupNumber[x[0]].push(fecha)
    groupNumber[x[0]].push(x)
  })
}
console.log(groupNumber)

First, you have two different code versions. The one in the link and the one you pasted here. Which one should we be looking at?

Second, what date array? I don’t see an array with the name date. Please be a little more clear about what you are trying to do.

In the code in the link, the getNumbers function is splitting the winning_numbers into the array arrayNumbers, so you could return that array from the function. But I’m not sure what you are trying to do with it after that?

The final solution should be something like this

groupNumber = {
   0: [ ],
   1: [ ],
   2: [ ],
   3: [ day: {
    '2022-22-03': [32,32],
    ...
    '2022-25-12': [30,33, 39]
  }] 
  ...
}

Sorry, I’m still unclear on what you are trying to do here. Instead of showing us your code perhaps you should try to explain in words what you need to do?

so I have this object

const data = [
{"draw_date":"2022-03-16T00:00:00.000","winning_numbers":"34 42 48 51 56 59","bonus":"6"}
]

and I want to create a data structure that I can group the winning numbers

"winning_numbers":"34 42 48 51 56 59"

with the day

"draw_date":"2022-03-16T00:00:00.000"

So I would have an object with keys that will group the winning numbers like 0 will group numbers from 0-9, 1 => 10-19, 2 => 20-29, … , 5 => 50-59

{0,1,2,3,4,5}

and each key will have an array of the winning_numbers with its date, there is only 1 row object in the example but I want to use like 1,000 rows

const newObject  = {
...
3: [ day: {
    '2022-22-03': [32,32],
    ...
    '2022-25-12': [30,33, 39]
  }]
...
}

OK, I think we both understand what you want to do now :slight_smile:

Next step, decide which code we should be looking at. As I mentioned above, you gave us two different versions. Please pick one. I would recommend the code in the replit, that way you can make changes and we can see your changes without you having to paste it in this thread.

So let’s assume we are using the replit code. You are iterating through the data array using map. What do you need to do for each iteration?

iight, I think i’m gonna go head to tackle this with a class. I update the code link

Anyone care to give a hand…

I don’t understand. Did you mean “a class” as in a school class or did you mean like a JS class?

And please don’t “update” links. Please provide new ones - it makes it confusing as to what people are referring to.

OK, looking at the above link (which I assume is the right one), it looks like you meant JS classes - when I first read that sentence I thought you were saying that this is a school assignment and you were going to work on it in class.

I’m still not clear on a few things. We’re ignoring the “bonus”? And we are guaranteed to not have duplicate dates?

And you still are using map incorrectly. It works but it is semantically incorrect. I mentioned this in the other thread. (Please don’t create multiple threads on the same subject by the way.) You should be using forEach in this case. map creates and returns a new array where each element is based on the corresponding old element. You just want to do something to/with each element. That is forEach.

The other issue I have is that in your sample output, I see this:

3: [ day: {

I don’t understand what the opening square bracket is. Is that the start of an array? Then the “day” shouldn’t be there because arrays don’t take key/value pairs.


That being said, I think classes are overkill here. I took a swipe at this, and I took the full data from your old post, with a spell correction:

const TEST_DATA = [
  { draw_date: '2022-03-16T00:00:00.000', winning_numbers: '34 42 48 51 56 59', bonus: '6' },
  { draw_date: '2022-03-12T00:00:00.000', winning_numbers: '12 19 22 34 50 56', bonus: '7' },
  { draw_date: '2022-03-09T00:00:00.000', winning_numbers: '17 28 29 32 37 40', bonus: '56' },
  { draw_date: '2022-03-05T00:00:00.000', winning_numbers: '01 11 21 38 39 52', bonus: '44' },
  { draw_date: '2022-03-02T00:00:00.000', winning_numbers: '19 20 37 41 47 55', bonus: '31' },
  { draw_date: '2022-02-26T00:00:00.000', winning_numbers: '08 32 43 46 52 58', bonus: '14' },
  { draw_date: '2022-02-23T00:00:00.000', winning_numbers: '13 15 36 46 53 57', bonus: '58' },
]

And I wrote a function and was able to pump it through and get this:

{
  '0': {
    '2022-03-05': [1],
    '2022-02-26': [8]
  },
  '1': {
    '2022-03-12': [12, 19],
    '2022-03-09': [17],
    '2022-03-05': [11],
    '2022-03-02': [19],
    '2022-02-23': [13, 15]
  },
  '2': {
    '2022-03-12': [22],
    '2022-03-09': [28, 29],
    '2022-03-05': [21],
    '2022-03-02': [20]
  },
  '3': {
    '2022-03-16': [34],
    '2022-03-12': [34],
    '2022-03-09': [32, 37],
    '2022-03-05': [38, 39],
    '2022-03-02': [37],
    '2022-02-26': [32],
    '2022-02-23': [36]
  },
  '4': {
    '2022-03-16': [42, 48],
    '2022-03-09': [40],
    '2022-03-02': [41, 47],
    '2022-02-26': [43, 46],
    '2022-02-23': [46]
  },
  '5': {
    '2022-03-16': [51, 56, 59],
    '2022-03-12': [50, 56],
    '2022-03-05': [52],
    '2022-03-02': [55],
    '2022-02-26': [52, 58],
    '2022-02-23': [53, 57]
  }
}

Is that the shape of data you are after?

This makes some assumptions - the number data will always be a two digit string with a leading 0 if needed, and we don’t care about the order of the numbers in the final array.

2 Likes

haha no not for school, I was doing it with functions first but, it started getting messy so, I switch to classes I think it looks better. I wanted to solve this so, I could learn how to manipulate data with Js and to understand what my weak topics are, I haven’t found much resources in how to do this just like basic adding and filtering online. and ultimately I want to draw it with d3js

  1. I am ignoring the bonus for now.
  2. There aren’t duplicate days in this one
  3. yeah that output looks much cleaner.

What skill am i lagging in JS to not be able to do this ?

I don’t want to gve away te solution anddeny you the opportunity to lean.

I think youhave enough JS to fo this. Just, like I said, don’t use map. If you’re not comfortable with prototype methods, then maybe just use for loops.

Basically I’m just looping over the data, parsing out my number data, then placing that number in the right place in the data object. The only slightly tricky part is checking to make sure that section of data has already been created. This not difficult JS, it’s just conceptually a little weird if you’re not used to transforming data, and there are a few tiny "goycha"s.

Give it a shot. It’s late here but tomorrow morning if you haven’t gotten it I’ll write out some pseudo code. We’ll see if you can get there without me blurting out the answer.

2 Likes

If I were to put this into pseudo code, it would be something like this:

loop draw data
  split numbers into array
  get digit key
  loop draw numbers
    if this digit key doesn't exist
      create digit key object on final object
    if digit key -> draw date array exists
      push draw number
    else
      create digit key -> draw date array with draw number as element

That’s pretty much it.

1 Like

I’m not getting line 6,

if this digit key doesn't exit
   create digit key object on (final object)

what do you mean by final object this is what I made lol

addDay(key,day, num){
    if(!this.keys[key]){
      this.dias[day] = []
      this.keys[key] = this.dias
    } 
    if(this.keys[key][day]){
      this.keys[key][day].push(num)
    } else {
      this.keys[key][day] = []
    }
  }

I meant “exist”. Please reread with that in mind.

1 Like

Nice, thank you man, I’m almost there its starting to make more sense now. Now I got The keys 0, 1 and 2 overlapping with the array from the 50s I think on the first run the array from 50s is not reading the correct key.

addDay(key,day, num){
    if(!this.keys[key]){
      this.days[day] = []
      this.keys[key] = {...this.days}
    }
    if(this.keys[key][day]){
      this.keys[key][day].push(num)
    } else {
      this.keys[key][day] = [num]
    }   
  }

I fucking got it man, shit it feels good. Thank you man appreciated. fixed the bug on my phone trying to go to sleep instead of watching social media. 9 lines of code in 4 days I suck lol

addDayKeyNum(key,day, num){
    if(!this.keys[key]){
      this.keys[key] = {}
    }
    if(this.keys[key][day]){
      this.keys[key][day].push(+num)
    } else {
      this.keys[key][day] = [+num]
    }   
  }

No, this is hard stuff. This takes a while for anyone. Be proud that you got it, don’t worry that it took a while. The next one will be a little easier, and so on.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.