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

5
  • Inside the reduce callback (predicate) you are returning an object, so the reduce call returns an object. Commented Apr 4, 2022 at 14:47
  • You need to give input and output expectations in your question. That would help in understanding which problem you're facing Commented Apr 4, 2022 at 14:51
  • @kellys can you indicate where? i don't get it Commented Apr 4, 2022 at 14:58
  • @NickVu i edited and provided both expectations. thanks Commented Apr 4, 2022 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] Commented Apr 4, 2022 at 14:59

1 Answer 1

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])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.