This is my Array with data:
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
}
]
I want to match array element by another array, for example:
let match= [1022692502, 1012182502]
In this case expected result is:
[
{
"fineAmount": 20
},
{
"fineAmount": 0
}
]
How can I do this?
To check if multiple values exist in an array:Use the every() method to iterate over the array of values. On each iteration, use the indexOf method to check if the value is contained in the other array. If all values exist in the array, the every method will return true .
Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.
JavaScript finding non-matching values in two arrays The code will look like this. const array1 = [1, 2, 3, 4, 5, 6]; const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const output = array2. filter(function (obj) { return array1. indexOf(obj) === -1; }); console.
You should try doing this using Array.filter
& Array.map
. The Array.filter
will test whether the feeSubHeadId
is included in the match
.
Then you can proceed to extract the fineAmount
property in the 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);
We could use Array.from
also, the first parameter is the filtered array the second one will map it to the new object:
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);
You could use reduce to do this
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With