我如何在VUE3中过滤一个数组

发布于 2025-02-13 22:10:47 字数 501 浏览 0 评论 0原文

我是Vue3的新手。

我必须过滤一个带有相同项目的数组,然后将结果推入另一个数组,

我的数组是

let list = [
  {"name":"1000","properties":{"item":"1","unit":"DZN"}},
  {"name":"2000","properties":{"item":"1","unit":"CTN"}},
  {"name":"3000","properties":{"item":"2","unit":"DZN"}},
  {"name":"4000","properties":{"item":"3","unit":"CTN"}}
]

我需要在另一个数组中使用条件item = 1的相应名称。

结果数组将类似于[{“ name”:“ 1000”,“单元”:“ dzn”},{“ name”:“ 2000”,“ unit”:“ ctn”}]>

tia

I am new to Vue3.

I have to filter a array with same item and push results into another array

My array is

let list = [
  {"name":"1000","properties":{"item":"1","unit":"DZN"}},
  {"name":"2000","properties":{"item":"1","unit":"CTN"}},
  {"name":"3000","properties":{"item":"2","unit":"DZN"}},
  {"name":"4000","properties":{"item":"3","unit":"CTN"}}
]

I need corresponding name with condition item =1 in another array.

Result array will be similar to [{"name":"1000", "unit":"DZN"}, {"name":"2000", "unit":"CTN"}]

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独陪着我 2025-02-20 22:10:47

如果我正确理解,您想从具有 item item 值等于1的记录中创建具有 name unit 属性的对象数组您必须首先过滤一个数组才能获取所需值的记录 item ,然后映射这些值以创建新的对象数组。

这是一个示例:

let list = [
  {"name":"1000","properties":{"item":"1","unit":"DZN"}},
  {"name":"2000","properties":{"item":"1","unit":"CTN"}},
  {"name":"3000","properties":{"item":"2","unit":"DZN"}},
  {"name":"4000","properties":{"item":"3","unit":"CTN"}}
]

const filteredList = list.filter((e) => e.properties.item === "1").map((e) => { return {name: e.name, unit: e.properties.unit}});
console.log(filteredList);

如果这是一个Vue3反应变量,请记住在filter()方法之前添加 .value

If I understand correctly you want to create an array of objects with name and unit attributes from records which have item values equal to 1. To do that you have to first filter an array to get only records which have desired value of item and then map those values to create a new array of objects.

Here is an example:

let list = [
  {"name":"1000","properties":{"item":"1","unit":"DZN"}},
  {"name":"2000","properties":{"item":"1","unit":"CTN"}},
  {"name":"3000","properties":{"item":"2","unit":"DZN"}},
  {"name":"4000","properties":{"item":"3","unit":"CTN"}}
]

const filteredList = list.filter((e) => e.properties.item === "1").map((e) => { return {name: e.name, unit: e.properties.unit}});
console.log(filteredList);

If this is a Vue3 reactive variable remember to add .value before filter() method to make it work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文