简体   繁体   中英

looping through and array object and creating an other object jquery js

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. 图片

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM