如何使用Angular从多个对象的列表中获取单个值



我有下面列出的4组对象,我需要取名称"样品4〃;来自第二组对象(Id:2(。如何使用javascript/angular获取此值。

{Id: 1, name: sample 1, code: "type", order: 1}
{Id: 1, name: sample 2, code: "type1", order: 2}
{Id: 1, name: sample 3, code: "type2", order: 3}
{Id: 2, name: sample 1, code: "type", order: 1}
{Id: 2, name: sample 2, code: "type1", order: 2}
{Id: 2, name: sample 3, code: "type2", order: 3}
{Id: 2, name: sample 4, code: "type3", order: 4}
{Id: 3, name: sample 1, code: "type", order: 1}
{Id: 3, name: sample 2, code: "type1", order: 2}
{Id: 3, name: sample 3, code: "type2", order: 3}
{Id: 4, name: sample 1, code: "type", order: 1}
{Id: 4, name: sample 2, code: "type1", order: 2}
{Id: 4, name: sample 3, code: "type2", order: 3}

使用数组过滤器,条件是id为2,名称为"sample 4"。这将返回另一个数组。然后您可以从中检索值

let data = [{
Id: 1,
name: 'sample 1',
code: "type",
order: 1
},
{
Id: 1,
name: 'sample 2',
code: "type1",
order: 2
},
{
Id: 1,
name: 'sample 3',
code: "type2",
order: 3
},
{
Id: 2,
name: 'sample 1',
code: "type",
order: 1
},
{
Id: 2,
name: 'sample 2',
code: "type1",
order: 2
},
{
Id: 2,
name: 'sample 3',
code: "type2",
order: 3
},
{
Id: 2,
name: 'sample 4',
code: "type3",
order: 4
},
{
Id: 3,
name: 'sample 1',
code: "type",
order: 1
},
{
Id: 3,
name: 'sample 2',
code: "type1",
order: 2
},
{
Id: 3,
name: 'sample 3',
code: "type2",
order: 3
},
{
Id: 4,
name: 'sample 1',
code: "type",
order: 1
},
{
Id: 4,
name: 'sample 2',
code: "type1",
order: 2
},
{
Id: 4,
name: 'sample 3',
code: "type2",
order: 3
}
];
let newData = data.filter(item => item.Id === 2 && item.name === 'sample 4');
console.log(newData)

相关内容

  • 没有找到相关文章

最新更新