如何在JSONata中删除数组的第一个元素?

huangapple go评论55阅读模式
英文:

How to remove the first element of an array in JSONata?

问题

要删除类似于以下内容的数组的第一个元素,可以使用以下方法:

{
  "array":[
    {
      "item":"1"
    },
    {
      "item":"2"
    },
    {
      "item":"3"
    }
  ]
}

期望的结果如下:

{
  "array":[    
    {
      "item":"2"
    },
    {
      "item":"3"
    }
  ]
}
英文:

How do I remove the first element of an array that looks like this:

{
  "array":[
    {
      "item":"1"
    },
    {
      "item":"2"
    },
    {
      "item":"3"
    }
  ]
}

I am expecting it to look like this:

{
  "array":[    
    {
      "item":"2"
    },
    {
      "item":"3"
    }
  ]
}

答案1

得分: 4

这可以通过$filter JSONata函数来实现:

{
  "array": $filter(array, function ($v, $i) {
    $i != 0
  })
}

JSONata playground中尝试它。

英文:

This can be accomplished via the $filter JSONata function:

{
  "array": $filter(array, function ($v, $i) {
    $i != 0
  })
}

try it here: JSONata playground

(edited to nest the output under the array key per the example output in the question)

答案2

得分: 1

你可以使用 filter 运算符 与一个 绑定的位置变量 结合起来,如下所示进行过滤:

{ "array": array#$i[$i>0] }

在 playground 上查看:https://stedi.link/5hQYetH

英文:

You can use the filter operator combined with a bound positional variable like this to filter it:

{ "array": array#$i[$i>0] }

Check it out on the playground: https://stedi.link/5hQYetH

答案3

得分: 0

你可以使用 Array.prototype.shift()

const obj = {
  "array": [
    {
      "item": "1"
    },
    {
      "item": "2"
    },
    {
      "item": "3"
    }
  ]
};
const firstElement = obj.array.shift();
console.log(obj.array)
英文:

You can your Array.prototype.shift()
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const obj = {
  &quot;array&quot;:[
    {
      &quot;item&quot;:&quot;1&quot;
    },
    {
      &quot;item&quot;:&quot;2&quot;
    },
    {
      &quot;item&quot;:&quot;3&quot;
    }
  ]
};
const firstElement = obj.array.shift();
console.log(obj.array)

<!-- end snippet -->

答案4

得分: 0

你可以使用Array.prototype.filter()

const obj = {
   "array": [
       {
            "item": "1"
        },
        {
            "item": "2"
        },
        {
            "item": "3"
         }]
 }

obj.array = obj.array.filter((item, index) => index !== 0);

console.log(obj)
英文:

You can use Array.prototype.filter()

const obj = {
   &quot;array&quot;:[
       {
            &quot;item&quot;:&quot;1&quot;
        },
        {
            &quot;item&quot;:&quot;2&quot;
        },
        {
            &quot;item&quot;:&quot;3&quot;
         }]}
 
obj.array = obj.array.filter((item , index) =&gt; index !== 0);

console.log(obj)

答案5

得分: 0

你可以使用展开运算符 ... 结合解构语法。第一个“空”逗号元素将被忽略,其余元素将被赋值给 newArray

const data = {
  "array": [
    {
      "item": "1"
    },
    {
      "item": "2"
    },
    {
      "item": "3"
    }
  ]
};

const { array: [, ...newArray] } = data;

console.log(newArray);

请注意,这是一段JavaScript代码示例。

英文:

You can use destructuring syntax with spread operator .... The first "empty" comma element is ignored. The rest of elements are assigned to newArray
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = {
  &quot;array&quot;:[
    {
      &quot;item&quot;:&quot;1&quot;
    },
    {
      &quot;item&quot;:&quot;2&quot;
    },
    {
      &quot;item&quot;:&quot;3&quot;
    }
  ]
};

const { array: [, ...newArray] } = data;

console.log(newArray);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月1日 11:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定