0

Sorry guys if my way of asking the question in the title is not correct. I am working on a project on react js and I am getting the data like this

[
    {
        "count": [
            {
                "1": 16
            },

            {
                "1": 149
            }

        ],
        "day": "2019-08-27"
    }
]

now this is my first time I am dealing with this kind of data and I really have no idea how can I show it like this I am really sorry guys I literally can't even show what I have tried because it does not seem relevant

[
    {
       count: 165
       day:"2019-08-27"
    }
}
1
  • Look up JSON Objects Commented Feb 13, 2020 at 10:05

2 Answers 2

1

Assuming the data you're getting is under a variable called data you could use reduce:

The below makes the assumption the count is always an array of objects with just 1 key called '1'.

const newData = data.map(datum => {
  datum.count = datum.count.reduce((count, item) => {
    return count + item['1']
  }, 0)

  return datum
})
Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this:

let arr = [
  // item
  {
    count: [
      {
        "1": 16
      },

      {
        "1": 149
      }
    ],
    day: "2019-08-27"
  }
];

arr.map(item => {
  Object.keys(item).map(key => {
    console.log(item[key])
    // if item[key] is iterable
    if(Array.isArray(item[key])) {
      item[key].map(val => {
        console.log(item)
      })
    } else {
      console.log(item[key])
    }
  });
});

The concept is that for Objects you do a Object.keys().something and for an array you do a arr.map(item => ...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.