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

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

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

问题

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

  1. {
  2. "array":[
  3. {
  4. "item":"1"
  5. },
  6. {
  7. "item":"2"
  8. },
  9. {
  10. "item":"3"
  11. }
  12. ]
  13. }

期望的结果如下:

  1. {
  2. "array":[
  3. {
  4. "item":"2"
  5. },
  6. {
  7. "item":"3"
  8. }
  9. ]
  10. }
英文:

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

  1. {
  2. "array":[
  3. {
  4. "item":"1"
  5. },
  6. {
  7. "item":"2"
  8. },
  9. {
  10. "item":"3"
  11. }
  12. ]
  13. }

I am expecting it to look like this:

  1. {
  2. "array":[
  3. {
  4. "item":"2"
  5. },
  6. {
  7. "item":"3"
  8. }
  9. ]
  10. }

答案1

得分: 4

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

  1. {
  2. "array": $filter(array, function ($v, $i) {
  3. $i != 0
  4. })
  5. }

JSONata playground中尝试它。

英文:

This can be accomplished via the $filter JSONata function:

  1. {
  2. "array": $filter(array, function ($v, $i) {
  3. $i != 0
  4. })
  5. }

try it here: JSONata playground

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

答案2

得分: 1

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

  1. { "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:

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

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

答案3

得分: 0

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

  1. const obj = {
  2. "array": [
  3. {
  4. "item": "1"
  5. },
  6. {
  7. "item": "2"
  8. },
  9. {
  10. "item": "3"
  11. }
  12. ]
  13. };
  14. const firstElement = obj.array.shift();
  15. console.log(obj.array)
英文:

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

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

  1. const obj = {
  2. &quot;array&quot;:[
  3. {
  4. &quot;item&quot;:&quot;1&quot;
  5. },
  6. {
  7. &quot;item&quot;:&quot;2&quot;
  8. },
  9. {
  10. &quot;item&quot;:&quot;3&quot;
  11. }
  12. ]
  13. };
  14. const firstElement = obj.array.shift();
  15. console.log(obj.array)

<!-- end snippet -->

答案4

得分: 0

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

  1. const obj = {
  2. "array": [
  3. {
  4. "item": "1"
  5. },
  6. {
  7. "item": "2"
  8. },
  9. {
  10. "item": "3"
  11. }]
  12. }
  13. obj.array = obj.array.filter((item, index) => index !== 0);
  14. console.log(obj)
英文:

You can use Array.prototype.filter()

  1. const obj = {
  2. &quot;array&quot;:[
  3. {
  4. &quot;item&quot;:&quot;1&quot;
  5. },
  6. {
  7. &quot;item&quot;:&quot;2&quot;
  8. },
  9. {
  10. &quot;item&quot;:&quot;3&quot;
  11. }]}
  12. obj.array = obj.array.filter((item , index) =&gt; index !== 0);
  13. console.log(obj)

答案5

得分: 0

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

  1. const data = {
  2. "array": [
  3. {
  4. "item": "1"
  5. },
  6. {
  7. "item": "2"
  8. },
  9. {
  10. "item": "3"
  11. }
  12. ]
  13. };
  14. const { array: [, ...newArray] } = data;
  15. 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 -->

  1. const data = {
  2. &quot;array&quot;:[
  3. {
  4. &quot;item&quot;:&quot;1&quot;
  5. },
  6. {
  7. &quot;item&quot;:&quot;2&quot;
  8. },
  9. {
  10. &quot;item&quot;:&quot;3&quot;
  11. }
  12. ]
  13. };
  14. const { array: [, ...newArray] } = data;
  15. 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:

确定