0

I'm using reduce function and i expect an array to be returned, instead is returning an object. Here's my code so far:

const values = useMemo(() => {
    const result = value.reduce((acc, inc, i) => {
      return {
        ...acc,
        ...Object.values(inc.table).filter((t) => {
          const newObj = _omit(t, ['keyword'])
          const { operator, value } =
            categoriesMap[config[i].category].summary

          const isValid = Object.values(newObj).every(item => item >= value)

          if (
            !(
              categoriesMap[config[i].category].summary &&
              categoriesMap[config[i].category].summary.enable
            )
          ) return false;

          if (isValid) {
            return true
          }
          else {
            return false
          }

        }).map((r) => {
          const newObj = _omit(r, ['keyword'])
          const valueObj = Object.values(newObj).map((i) => i)
          const max = Math.max(...valueObj)
          return {
            [r.keyword]: max
          }
        })
      };
    }, [])
    return result
  }, [config, value, categoriesMap])

Note that value is an array of objects like this:

[{
keyword: 'keyword',
today:9,
yesterday: 8
}]

Here's a log of what values is at the end (result is exactly the same of course):

0: {…}, 1: {…}}0: Programmazione: 8[[Prototype]]: Object1: Robotica: 9[[Prototype]]: Object[[Prototype]]: Object

What i expect :

(2) [{…}, {…}]
0:
Programmazione: 8
[[Prototype]]: Object
1:
Robotica: 9
[[Prototype]]: Object
length: 2
[[Prototype]]: Array(0)

Am i missing something in the reduce function? Any tips are appreciated

Levi D.
  • 176
  • 8
  • Inside the reduce callback (predicate) you are returning an object, so the reduce call returns an object. – kelsny Apr 04 '22 at 14:47
  • You need to give input and output expectations in your question. That would help in understanding which problem you're facing – Nick Vu Apr 04 '22 at 14:51
  • @kellys can you indicate where? i don't get it – Levi D. Apr 04 '22 at 14:58
  • @NickVu i edited and provided both expectations. thanks – Levi D. Apr 04 '22 at 14:58
  • 1
    `return {...acc, otherStuff}` this returns an object, because of the curly braces. If you wanted to return an array try `return [...acc, otherStuff]` – James Apr 04 '22 at 14:59

1 Answers1

1

This should work

const values = useMemo(() => {
    const result = value.reduce((acc, inc, i) => {
      return [
        ...acc,
        ...Object.values(inc.table).filter((t) => {
          const newObj = _omit(t, ['keyword'])
          const { operator, value } =
            categoriesMap[config[i].category].summary

          const isValid = Object.values(newObj).every(item => item >= value)

          if (
            !(
              categoriesMap[config[i].category].summary &&
              categoriesMap[config[i].category].summary.enable
            )
          ) return false;

          if (isValid) {
            return true
          }
          else {
            return false
          }

        }).map((r) => {
          const newObj = _omit(r, ['keyword'])
          const valueObj = Object.values(newObj).map((i) => i)
          const max = Math.max(...valueObj)
          return {
            [r.keyword]: max
          }
        })
      ];
    }, [])
    return result
  }, [config, value, categoriesMap])
Max Filippov
  • 2,024
  • 18
  • 37