Home > Software design >  recreate array with all items but the last
recreate array with all items but the last

Time:11-25

Given the following:

Input:

[
  [
    {
      "type": "strip",
      "output": "debian-7-x86_64-strip"
    },
    {
      "type": "compress",
      "output": "debian-7-x86_64-compress"
    },
    {
      "type": "full",
      "output": "debian-7-x86_64"
    }
  ],
  [
    {
      "type": "strip",
      "output": "debian-8-strip"
    },
    {
      "type": "compress",
      "output": "debian-8-compress"
    }
  ]
]

How to recreate the array with the last object item (from each sub-array) removed?

Desired Output:

[
  [
    {
      "type": "strip",
      "output": "debian-7-x86_64-strip"
    },
    {
      "type": "compress",
      "output": "debian-7-x86_64-compress"
    }
  ],
  [
    {
      "type": "strip",
      "output": "debian-8-strip"
    }
  ]
]

I've tried using things like ... |= .[:-1] and ...[to_entries | last], and ... | select(last|not) but I'm having no success.

Many thanks for the help.

CodePudding user response:

.[][0:-1] gets you far, just missing the surrounding array in the output.

EDIT: [.[][:-1]] should do it.

CodePudding user response:

Try javascript pop function. The desired result will be the array arr.

const arr = [
  [
    {
      "type": "strip",
      "output": "debian-7-x86_64-strip"
    },
    {
      "type": "compress",
      "output": "debian-7-x86_64-compress"
    },
    {
      "type": "full",
      "output": "debian-7-x86_64"
    }
  ],
  [
    {
      "type": "strip",
      "output": "debian-8-strip"
    },
    {
      "type": "compress",
      "output": "debian-8-compress"
    }
  ]
]
for(var i=0;i<arr.length;i  ){
 arr[i].pop();
}
  • Related