Home > OS >  javascript: chart data - how to filter array of array with date
javascript: chart data - how to filter array of array with date

Time:09-30

I would like to reduce the amount of data to be charted. I have data with duplicate dates in an array of arrays.

How to keep only items with a single date?

I would like to have one array per day, the structure looks like this:

[
    [
        "date",
        "amount",
        "code"
    ],
    [
        "2022-08-18",
        4652,
        "AAA"
    ],
    [
        "2022-08-18",
        491783,
        "BBB"
    ]
    [
        "2022-08-19",
        515501,
        "AAA"
    ],
    [
        "2022-08-19",
        15953,
        "BBB"
    ],
    [
        "2022-08-19",
        15953,
        "CCC"
    ]
]

I tried

  temp1.filter((e, index)=> {
      if (index<temp1.length-1) {
        return e[0]==temp1[index 1][0] ? e : null
      } else {
        return e
      }
    })

But this only removes the last element before the new date (I shifted first index ofc).

CodePudding user response:

Check this (by the way, a comma is missing in your array :))

const data = [
  [
    'date',
    'amount',
    'code'
  ],
  [
    '2022-08-18',
    4652,
    'AAA'
  ],
  [
    '2022-08-18',
    491783,
    'BBB'
  ],
  [
    '2022-08-19',
    515501,
    'AAA'
  ],
  [
    '2022-08-19',
    15953,
    'BBB'
  ],
  [
    '2022-08-19',
    15953,
    'CCC'
  ]
]

function removeDuplicate (data) {
  const dateSet   = new Set()
  const cleanData = []
  data.forEach(item => {
    const date = item[0]
    if (dateSet.has(date)) return
    cleanData.push(item)
    dateSet.add(date)
  })
  return cleanData
}

console.log(removeDuplicate(data))

CodePudding user response:

Maybe the data structure you are using does not 100% fit the use case you are looking for. It might be better to use dictionaries, but this is not the topic here.

I think the confusion comes from the Array.filter method. The filter uses truthy/falsy values to determine, if an item will stay in the array. If it is truthy, the value stays, otherwise it gets removed. I'm not quite sure what you really want to have. If you only want 1 entry per day you have to decide, which from the other values you want to discard. The items need to be sorted by date in this case!

temp1.filter((e,i)=> (temp1[i 1][0]!=e[0])) //keeps the last item

temp1.filter((e,i)=> (temp1[i-1][0]!=e[0])) //keeps the first item

If you want to separate all items to their "date", maybe mapping the whole array to a dictionary is the way to go.

let itemsPerDate={}
temp1.forEach(e=>{
  if(itemsPerDate[e[0]])
    itemsPerDate[e[0]].push(e)
  else
    itemsPerDate[e[0]]=[e]
}) 

itemsPerDate then is 
{
  "date": [
    [
      "date",
      "amount",
      "code"
    ]
  ],
  "2022-08-18": [
    [
      "2022-08-18",
      4652,
      "AAA"
    ],
    [
      "2022-08-18",
      491783,
      "BBB"
    ]
  ],
  "2022-08-19": [
    [
      "2022-08-19",
      515501,
      "AAA"
    ],
    [
      "2022-08-19",
      15953,
      "BBB"
    ],
    [
      "2022-08-19",
      15953,
      "CCC"
    ]
  ]
}
  • Related