这是我的数据数组:
let myvalue= [
{
"feeSubHeadId": 1022692502,
"feeSubHeadName": "Quiz-01",
"fineAmount": 20
},
{
"feeSubHeadId": 1012182502,
"feeSubHeadName": "Half Yearly Exam",
"fineAmount": 0
},
{
"feeSubHeadId": 1022682502,
"feeSubHeadName": "Quiz-02",
"fineAmount": 0
},
{
"feeSubHeadId": 1012192502,
"feeSubHeadName": "Annual Exam",
"fineAmount": 0
}
]
我想用另一个数组来匹配数组元素,例如:
let match= [1022692502, 1012182502]
在这种情况下,预期结果是:
[
{
"fineAmount": 20
},
{
"fineAmount": 0
}
]
我该怎么做?
答案 0 :(得分:3)
您应该尝试使用Array.filter
和Array.map
进行此操作。 Array.filter
将测试feeSubHeadId
中是否包含match
。
然后,您可以继续在fineAmount
中提取Array.map
属性。
let myvalue= [
{
"feeSubHeadId": 1022692502,
"feeSubHeadName": "Quiz-01",
"fineAmount": 20
},
{
"feeSubHeadId": 1012182502,
"feeSubHeadName": "Half Yearly Exam",
"fineAmount": 0
},
{
"feeSubHeadId": 1022682502,
"feeSubHeadName": "Quiz-02",
"fineAmount": 0
},
{
"feeSubHeadId": 1012192502,
"feeSubHeadName": "Annual Exam",
"fineAmount": 0
}
];
let match= [1022692502, 1012182502];
const matchedItems = myvalue.filter(({feeSubHeadId}) => match.includes(feeSubHeadId)).map(({fineAmount})=> ({fineAmount}));
console.log(matchedItems);
我们也可以使用Array.from
,第一个参数是过滤后的数组,第二个参数将其映射到新对象:
let myvalue= [
{
"feeSubHeadId": 1022692502,
"feeSubHeadName": "Quiz-01",
"fineAmount": 20
},
{
"feeSubHeadId": 1012182502,
"feeSubHeadName": "Half Yearly Exam",
"fineAmount": 0
},
{
"feeSubHeadId": 1022682502,
"feeSubHeadName": "Quiz-02",
"fineAmount": 0
},
{
"feeSubHeadId": 1012192502,
"feeSubHeadName": "Annual Exam",
"fineAmount": 0
}
];
let match= [1022692502, 1012182502];
const matchedItems = Array.from(myvalue.filter(({feeSubHeadId}) => match.includes(feeSubHeadId)),
({fineAmount}) => ({fineAmount}));
console.log(matchedItems);
答案 1 :(得分:1)
您可以简单地使用reduce
和includes
let myvalue = [{"feeSubHeadId": 1022692502,"feeSubHeadName": "Quiz-01","fineAmount": 20},{"feeSubHeadId": 1012182502,"feeSubHeadName": "Half Yearly Exam","fineAmount": 0},{"feeSubHeadId": 1022682502,"feeSubHeadName": "Quiz-02","fineAmount": 0},{"feeSubHeadId": 1012192502,"feeSubHeadName": "Annual Exam","fineAmount": 0}]
let match= [1022692502, 1012182502]
let op = myvalue.reduce((op,{feeSubHeadId,fineAmount}) => {
if(match.includes(feeSubHeadId)){
op.push({fineAmount})
}
return op
},[])
console.log(op)
答案 2 :(得分:0)
使用Array#filter
方法过滤出所需的值,然后使用Array#map
方法格式化数组元素。
var res = myvalue
// filter out elements
.filter(({ feeSubHeadId }) => match.includes(feeSubHeadId))
// format array elements
.map(({ fineAmount }) => ({ fineAmount }));
let myvalue= [
{
"feeSubHeadId": 1022692502,
"feeSubHeadName": "Quiz-01",
"fineAmount": 20
},
{
"feeSubHeadId": 1012182502,
"feeSubHeadName": "Half Yearly Exam",
"fineAmount": 0
},
{
"feeSubHeadId": 1022682502,
"feeSubHeadName": "Quiz-02",
"fineAmount": 0
},
{
"feeSubHeadId": 1012192502,
"feeSubHeadName": "Annual Exam",
"fineAmount": 0
}
]
let match= [1022692502, 1012182502]
var res = myvalue
.filter(({ feeSubHeadId }) => match.includes(feeSubHeadId))
.map(({ fineAmount }) => ({ fineAmount }));
console.log(res);
或者使用Array#reduce
方法,您可以将两者结合在一起。
var res = myvalue
// iterate and push into array if id found
.reduce((arr, { feeSubHeadId,fineAmount }) => {
if (match.includes(feeSubHeadId)) arr.push({ fineAmount: fineAmount });
// return array reference for next iteration
return arr;
// set intial value as empty array for result
}, []);
let myvalue= [
{
"feeSubHeadId": 1022692502,
"feeSubHeadName": "Quiz-01",
"fineAmount": 20
},
{
"feeSubHeadId": 1012182502,
"feeSubHeadName": "Half Yearly Exam",
"fineAmount": 0
},
{
"feeSubHeadId": 1022682502,
"feeSubHeadName": "Quiz-02",
"fineAmount": 0
},
{
"feeSubHeadId": 1012192502,
"feeSubHeadName": "Annual Exam",
"fineAmount": 0
}
]
let match= [1022692502, 1012182502]
var res = myvalue
.reduce((arr, { feeSubHeadId,fineAmount }) => {
if (match.includes(feeSubHeadId)) arr.push({ fineAmount: fineAmount });
return arr;
}, []);
console.log(res);
为提取对象属性,我使用了ES6 Destructuring assignment功能。
答案 3 :(得分:0)
您可以使用reduce来完成
const res = myvalue.reduce((a, {feeSubHeadId, fineAmount}) =>
match.includes(feeSubHeadId) ? a.concat({fineAmount}) : a,[]
);
console.log(res);
<script>
let myvalue= [
{
"feeSubHeadId": 1022692502,
"feeSubHeadName": "Quiz-01",
"fineAmount": 20
},
{
"feeSubHeadId": 1012182502,
"feeSubHeadName": "Half Yearly Exam",
"fineAmount": 0
},
{
"feeSubHeadId": 1022682502,
"feeSubHeadName": "Quiz-02",
"fineAmount": 0
},
{
"feeSubHeadId": 1012192502,
"feeSubHeadName": "Annual Exam",
"fineAmount": 0
}
];
let match= [1022692502, 1022682502];
</script>