Given the input var key="value_0"
and var input="hello"
, I need to filter the array in TypeScript based on these values. The filtering criteria involve checking if the array elements contain a subarray with key="value_0"
and if the value of this key includes the input provided using the .includes
method.
public list: any = [{
"header": "API",
"data": [{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghn",
"value_4": "l"
},
{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
}
]
},
{
"header": "CLOUD",
"data": [{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
},
{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
}
]
},
{
"header": "OTHER HEADER",
"data": [{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
},
{
"value_0": "yello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
}
]
},
{
"header": "YET ANOTHER HEADER",
"data": [{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
},
{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
}
]
}
];
If the user provides key="value_o"
and input="hel"
, we need to filter the list by searching for the key within the data subarrays and then checking if the value includes the input provided. The final result will be an array of elements where both the key and the value match the user input. For example, filtering with key='value_0'
and input='hell'
will yield the same result as each index in the data attribute contains a subarray with the attribute value_0
and its value includes the input text.
[
{
"header": "API",
"data": [{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghn",
"value_4": "l"
},
{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
}
]
},
{
"header": "CLOUD",
"data": [{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
},
{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
}
]
},
{
"header": "OTHER HEADER",
"data": [{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
},
{
"value_0": "yello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
}
]
},
{
"header": "YET ANOTHER HEADER",
"data": [{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
},
{
"value_0": "hello",
"value_1": "abc",
"value_2": "def",
"value_3": "ghi",
"value_4": "jkl"
}
]
}
];