Object array filter

I have a object like this
var obj = {
key1: [
{ id: 1, f1: “x”, f2: “y”, f3: “z” },
{ id: 2, f1: “q”, f2: “w”, f3: “e” },
{ id: 3, f1: “a”, f2: “s”, f3: “d” }
],
key2: [
{ id: 1, f1: “z”, f2: “x”, f3: “c” },
{ id: 2, f1: “r”, f2: “t”, f3: “y” },
{ id: 4, f1: “f”, f2: “g”, f3: “h” }
],
key3: [
{ id: 2, f1: “b”, f2: “n”, f3: “m” },
{ id: 5, f1: “j”, f2: “k”, f3: “l” }
]
}
find equal id in each key
I want to a response like below, how can i do?
var res = {
key1: [
{ id: 2, f1: “q”, f2: “w”, f3: “e” }
],
key2: [
{ id: 2, f1: “r”, f2: “t”, f3: “y” }
],
key3: [
{ id: 2, f1: “b”, f2: “n”, f3: “m” }
]
}

Are you saying you always want the element of the each of obj’s properties (which are arrays) which has an id value of 2 to be in the res object?

yes, in this example id 2

You will need to create a function with a parameter to represent the id of the object you want to grab in all of the obj’s properties and then for each property in obj, you will need to iterate through its array until you find an object with corresponding id. Once found, you will use create a new property in an object named res which has an array of one element (the object you found).

Try to write the code now and let us know how it goes.