Home > Net >  How to filter the data containing array inside the data?
How to filter the data containing array inside the data?

Time:09-15

const arrayList = [{
  fNo: 343,
  origin: "Singapore",
  destination: "India",
  upType: ["Eco to PEco", "Eco to HClass"]
}, . {
  fNo: 363,
  origin: "Chennai",
  destination: "Singapore"
  upType: ["PEco to HClass"]
}]

const fiteredRulesData =
  arrayList.filter((data: any) => data.fNo.toString().includes(fNo.toString()))

&& ...similarly for other data like origin and deatination && how to do it for the upType array)

other is working but upType is not. Getting values fNo, origin and destination from the input but on selecting data getting upType as filterUpType = ["Eco to PEco"]

CodePudding user response:

You can use this filtering approach and play with .some, .every and .includes to achieve the desired result.

Live demo:

const arrayList = [{fNo: 343,origin: "Singapore",destination: "India",upType: ["one", "two"]}, 
{fNo: 363,origin: "Chennai",destination: "Singapore",upType: ["one", "three"]}, 
{fNo: 363,origin: "Chennai",destination: "Singapore",upType: ["two", "three"]}];

const upTypeTarget = ["one"];

const result = arrayList.filter(({ upType }) => upType.some(t => upTypeTarget.includes(t)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }

CodePudding user response:

    //you can use enter code here
    const filteredItems = arrayList.filter(item => item.upType && Array.isArray(item.upType) && item.upType.indexOf("Eco to PEco") !== -1)

  • Related