英文:
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 = {
  "array":[
    {
      "item":"1"
    },
    {
      "item":"2"
    },
    {
      "item":"3"
    }
  ]
};
const firstElement = obj.array.shift();
console.log(obj.array)
<!-- end snippet -->
答案4
得分: 0
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 = {
   "array":[
       {
            "item":"1"
        },
        {
            "item":"2"
        },
        {
            "item":"3"
         }]}
 
obj.array = obj.array.filter((item , index) => 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 = {
  "array":[
    {
      "item":"1"
    },
    {
      "item":"2"
    },
    {
      "item":"3"
    }
  ]
};
const { array: [, ...newArray] } = data;
console.log(newArray);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论