0

I am trying to filter some records coming from my api data, i am trying to use filter but i am not sure how should i do for multipoe items.

Here is my code the api response i have and what happens when i do a checkbox

my APi data

{
    "requestId": "",
    "totalRecords": 132,
    "status": "SUCCESS",
    "items": [
        {
            "Id": 3489,
            "Status":"Awaiting Funds",
            "amountPaid": {
                "unit": "CAD",
                "value": 10
            },
            "Name": "John Doe"
        },
        {
            "Id": 3508
            "Status":"Awaiting Funds",
            "amountPaid": {
                "unit": "CAD",
                "value": 10
            },
            "Name": "John Doe"
        },
        {
            "Id": 3503,
            "Status":"Awaiting Funds",
            "amountPaid": {
                "unit": "CAD",
                "value": 25
            },
            "Name": "John Sinth"
        }
    ]
}

and in my checkbnox i am passing two checkboxes values which are full object like 3508,3503 but now sure how to do

Here is my code as to what i did but i am not getting filetered records

const parsedData = JSON.parse(JSON.stringify(this.apiData));
                    if (this.multipleSelection.length > 0) {
                        const data = parsedData.filter(function (el)
                        {
                            return el.Id = 3489 and 3503
                        })
                    } else {
                        const data = parsedData;
                    }
9
  • If you do: console.log(typeof this.apiData) what do you get? Commented Jan 18, 2023 at 1:09
  • 3489 and 3503 is invalid JS. Please refer to some documentation. Commented Jan 18, 2023 at 1:11
  • Also learn about the difference of = and === Commented Jan 18, 2023 at 1:11
  • i know its invalid, but i want to know how do i get the only two objects when i am using filter instead of all 3 Commented Jan 18, 2023 at 1:11
  • i listed what type of api.Dataq is Commented Jan 18, 2023 at 1:12

1 Answer 1

1

Use Array.prototype.filter() in combination with Array.prototype.includes()

const data = {
    "requestId": "",
    "totalRecords": 132,
    "status": "SUCCESS",
    "items": [
        {
            "Id": 3489,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 10},
            "Name": "John Doe"
        },{
            "Id": 3508,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 10},
            "Name": "John Doe"
        },{
            "Id": 3503,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 25},
            "Name": "John Sinth"
        }
    ]
};

const ids = [3503, 3508];
const parsedData = data.items.filter((item) => ids.includes(item.Id));

console.log(parsedData)

if the above is still not clear, here's an example that uses checkboxes:

const data = {
    "requestId": "",
    "totalRecords": 132,
    "status": "SUCCESS",
    "items": [
        {
            "Id": 111,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 10},
            "Name": "John Doe"
        },{
            "Id": 222,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 10},
            "Name": "John Doe"
        },{
            "Id": 333,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 25},
            "Name": "John Sinth"
        }
    ]
};


const filterByIds = (ids) => data.items.filter((item) => ids.includes(+item.Id));

const elsIdInputs = document.querySelectorAll("[name='id']");


elsIdInputs.forEach((elId) => {
  elId.addEventListener("input", () => {
    const ids = [...elsIdInputs].filter(el => el.checked).map(el => +el.value);
    console.log(ids)
    if (!ids.length) return; // Exit. (none is checked)
    
    // Else... do something here
    console.clear();
    console.log(filterByIds(ids));
  });
});
<label><input type="checkbox" name="id" value="111"> 111</label>
<label><input type="checkbox" name="id" value="222"> 222</label>
<label><input type="checkbox" name="id" value="333"> 333</label>

Sign up to request clarification or add additional context in comments.

10 Comments

but i have an object, no specify ids with me, so i cannot directly add ids
@Nocus could you share an example of how that "object" looks like?
when i check the chbox, the value is going as array of object as: [{ "Id": 3503, "Status":"Awaiting Funds", "amountPaid": { "unit": "CAD", "value": 25 }, "Name": "John Sinth" }]
if i select 2 checkboxes, it pass 2 objects inside an array
i shared with you, how it looks like, if i select one checkbox, it passes the whole row as array of objects and one of such key is id and has a value of 3503
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.