[Solved] How to move object’s entries one up?


One option is to use a basic for loop, do some checks, then shift it onto the previous item.

const keys = Object.keys(obj)
for (let i in keys)
  if (
    typeof obj[keys[i]][0] !== 'undefined' && 
    typeof obj[keys[i-1]] !== 'undefined' && 
    obj[keys[i]][0].type === 'close'
  )
    obj[keys[i-1]].push(obj[keys[i]].shift())

const obj = {
  "monday": [],
  "tuesday": [{
      "type": "open",
      "value": 36000
    },
    {
      "type": "close",
      "value": 64800
    }
  ],
  "wednesday": [],
  "thursday": [{
      "type": "open",
      "value": 36000
    },
    {
      "type": "close",
      "value": 64800
    }
  ],
  "friday": [{
    "type": "open",
    "value": 36000
  }],
  "saturday": [{
      "type": "close",
      "value": 3600
    },
    {
      "type": "open",
      "value": 32400
    },
    {
      "type": "close",
      "value": 39600
    },
    {
      "type": "open",
      "value": 57600
    }
  ],
  "sunday": [{
      "type": "close",
      "value": 3600
    },
    {
      "type": "open",
      "value": 43200
    },
    {
      "type": "close",
      "value": 75600
    }
  ]
}

const keys = Object.keys(obj)
for (let i in keys)
  if (
    typeof obj[keys[i]][0] !== 'undefined' &&
    typeof obj[keys[i - 1]] !== 'undefined' &&
    obj[keys[i]][0].type === 'close'
  )
    obj[keys[i - 1]].push(obj[keys[i]].shift())
    
console.log(obj)

2

solved How to move object’s entries one up?