skip to Main Content

I am currently looking for a better way to filter my array of objects to avoid duplicate my code again and again.

Here is my example array:

arr = [
{
"no": 11145,
"stringers": "P1",
"ribs": "R1",
"description": "some text"
},
{
"no": 14568,
"stringers": "P1",
"ribs": "R1",
"description": "some text"
},
{
"no": 24562,
"stringers": "P2",
"ribs": "R9",
"description": "some text"
},
{
"no": 658741,
"stringers": "P1",
"ribs": "R2",
"description": "some text"
},
{
"no": 325690,
"stringers": "P4",
"ribs": "R5",
"description": "some text"
},
{
"no": 9745201,
"stringers": "P1",
"ribs": "R2",
"description": "some text"
},
.....
]

Currently I am filtering the array like that:

let p1r1 = arr.filter(function(el){
  return el.stringers === "P1" && el.ribs === "R1"
})
let p1r2 = arr.filter(function(el){
  return el.stringers === "P1" && el.ribs === "R2"
})
// ...and so on

This results in a lot of lines where I repeat the same code over and over (except for the changing ribs value). Is there a more elegant way to solve this?

2

Answers


  1. You can use array map to get just the ribs value.

    arr.map(a => a.ribs); // [R1, R2, R1...]
    

    A set is like an array, but only of unique values.

    new Set(arr.map(a => a.ribs))
    

    I like using array functions, so I cast that back to an array

    const uniqueRibs = Array.from(new Set(arr.map(a => a.ribs)))
    

    Now we can do anything with that.

    uniqueRibs.forEach(ribs => console.log(arr.filter(a => a.ribs == ribs)))
    

    Runnable example:

    const arr = [
    {
    "no": 11145,
    "stringers": "P1",
    "ribs": "R1",
    "description": "some text"
    },
    {
    "no": 14568,
    "stringers": "P1",
    "ribs": "R1",
    "description": "some text"
    },
    {
    "no": 24562,
    "stringers": "P2",
    "ribs": "R9",
    "description": "some text"
    },
    {
    "no": 658741,
    "stringers": "P1",
    "ribs": "R2",
    "description": "some text"
    },
    {
    "no": 325690,
    "stringers": "P4",
    "ribs": "R5",
    "description": "some text"
    },
    {
    "no": 9745201,
    "stringers": "P1",
    "ribs": "R2",
    "description": "some text"
    },
    ]
    
    Array.from(new Set(arr.map(a => a.ribs))).forEach(ribs => {
      console.log(`Ribs with value ${ribs}`);
      console.log(arr.filter(a => a.ribs == ribs));
    });
    Login or Signup to reply.
  2. You could take an object as result with the wanted keys as groups.

    const
        data = [{ no: 11145, stringers: "P1", ribs: "R1", description: "some text" }, { no: 14568, stringers: "P1", ribs: "R1", description: "some text" }, { no: 24562, stringers: "P2", ribs: "R9", description: "some text" }, { no: 658741, stringers: "P1", ribs: "R2", description: "some text" }, { no: 325690, stringers: "P4", ribs: "R5", description: "some text" }, { no: 9745201, stringers: "P1", ribs: "R2", description: "some text" }],
        result = Object.groupBy(data, ({ stringers, ribs }) => stringers + ribs);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search