Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Match from Multiple Array Value

Tags:

javascript

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?

like image 646
todd Avatar asked Mar 24 '19 10:03

todd


People also ask

How do you check if an Array contains multiple values?

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 .

How do you compare two values in an Array?

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.

How can I find not matching values in two arrays?

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.


2 Answers

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);
like image 98
Fullstack Guy Avatar answered Oct 20 '22 18:10

Fullstack Guy


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>
like image 1
baao Avatar answered Oct 20 '22 18:10

baao