0

I am trying to create a JSON object such as

{
  "measure": camera_name,
  "interval_s": 7 * 24 * 60 * 60,
  "data": [
      [done_at, action],
      [done_at, action],
      [done_at, action]
  ]
}

my current object is an array from which I want to achieve the above one. Image

here is the situation: my array object has many objects as in picture am only showing one object, and there are going to be many logs in one object and I want to loop through this as well to set the in "data". the resulting json object will be something as

{
  "measure": camera_name,
  "interval_s": 7 * 24 * 60 * 60,
  "data": [
      [done_at, action],
      [done_at, action],
      [done_at, action]
  ]
},
{
  "measure": camera_name,
  "interval_s": 7 * 24 * 60 * 60,
  "data": [
      [done_at, action],
      [done_at, action],
      [done_at, action]
  ]
}

How i can achieve that? I can loop through data but I have no idea how to store such json object. any help will be thankful

1 Answer 1

1

If ES6 is available to you, you can do this:

const result = data.map(entry => {
  return {
    measure: entry.camera_name,
    interval_s: 7 * 24 * 60 * 60,
    data: entry.logs.map(log => {
      return [log.done_at, log.action]
    })
  }
})
console.log(JSON.stringify(result))
Sign up to request clarification or add additional context in comments.

6 Comments

and If its not available? and Even its not available to me. i need it js or jquery
Even i converted it to JS and then the results are empty array
You set your data to the "data" variable and the result was an empty array?
ES6 is a language specification for javascript, what I wrote above is valid JavaScript, no jQuery needed.
i.imgsafe.org/e6f6686ff9.png the above one is my object and lower ones is yours result. and it returning an array object no json
|

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.