skip to Main Content

I had an object with this structure:

[
  {
    "event_id": 1,
    "subjects": [
      {
        "id": 12,
        "name": "Chemistry"
      },
      {
        "id": 13,
        "name": "Physics"
      },
      {
        "id": 14,
        "name": "Psychology"
      },
      {
        "id": 16,
        "name": "History"
      }
    ]
  },
  {
    "event_id": 2,
    "subjects": [
      {
        "id": 11,
        "name": "Maths"
      },
      {
        "id": 12,
        "name": "Chemistry"
      },
      {
        "id": 14,
        "name": "Biology"
      },
      {
        "id": 15,
        "name": "Geography"
      }
    ]
  },
  {
    "event_id": 3,
    "subjects": [
      {
        "id": 14,
        "name": "Biology"
      },
      {
        "id": 15,
        "name": "Geography"
      },
      {
        "id": 16,
        "name": "History"
      }
    ]
  }
]

What will be the fastest way to get all possible values of subjects prop in this option? Is this only possible to achieve by looping through each single sub-object and push value to "counting array" if it not already there?

3

Answers


  1. The simplest and most readable way would indeed be to loop through the subjects and add them to the array if they don’t exist.

    const data = [
      {
        "event_id": 1,
        "subjects": [
          {
            "id": 12,
            "name": "Chemistry"
          },
          {
            "id": 13,
            "name": "Physics"
          },
          {
            "id": 14,
            "name": "Psychology"
          },
          {
            "id": 16,
            "name": "History"
          }
        ]
      },
      {
        "event_id": 2,
        "subjects": [
          {
            "id": 11,
            "name": "Maths"
          },
          {
            "id": 12,
            "name": "Chemistry"
          },
          {
            "id": 14,
            "name": "Biology"
          },
          {
            "id": 15,
            "name": "Geography"
          }
        ]
      },
      {
        "event_id": 3,
        "subjects": [
          {
            "id": 14,
            "name": "Biology"
          },
          {
            "id": 15,
            "name": "Geography"
          },
          {
            "id": 16,
            "name": "History"
          }
        ]
      }
    ]
    
    const all = []
    for (const { subjects } of data) {
      subjects.forEach((s) => {
        if (all.indexOf(s.name) === -1) {
          all.push(s.name)
        }
      })
    }
    console.log(all)

    Another useful way is to use a Set with spread syntax to deduplicate the values in the array.

    const data = [
      {
        "event_id": 1,
        "subjects": [
          {
            "id": 12,
            "name": "Chemistry"
          },
          {
            "id": 13,
            "name": "Physics"
          },
          {
            "id": 14,
            "name": "Psychology"
          },
          {
            "id": 16,
            "name": "History"
          }
        ]
      },
      {
        "event_id": 2,
        "subjects": [
          {
            "id": 11,
            "name": "Maths"
          },
          {
            "id": 12,
            "name": "Chemistry"
          },
          {
            "id": 14,
            "name": "Biology"
          },
          {
            "id": 15,
            "name": "Geography"
          }
        ]
      },
      {
        "event_id": 3,
        "subjects": [
          {
            "id": 14,
            "name": "Biology"
          },
          {
            "id": 15,
            "name": "Geography"
          },
          {
            "id": 16,
            "name": "History"
          }
        ]
      }
    ]
    
    const all = []
    for (const { subjects } of data) {
      subjects.forEach((s) => {
        all.push(s.name)
      })
    }
    const s = [...new Set(all)]
    console.log(s)
    Login or Signup to reply.
  2. just because there’s several ways to do the same things, here’s one with reduce

    data.reduce((accumulator, currentValue) => {
        accumulator.push(currentValue.subjects.map(x => x.name));
        return [...new Set(accumulator.flatMap(x => x))];
    }, [])
    
    
    // will output
    // ['Chemistry', 'Physics', 'Psychology', 'History', 'Maths', 'Biology', 'Geography']
    
    const data = [
      {
        "event_id": 1,
        "subjects": [
          {
            "id": 12,
            "name": "Chemistry"
          },
          {
            "id": 13,
            "name": "Physics"
          },
          {
            "id": 14,
            "name": "Psychology"
          },
          {
            "id": 16,
            "name": "History"
          }
        ]
      },
      {
        "event_id": 2,
        "subjects": [
          {
            "id": 11,
            "name": "Maths"
          },
          {
            "id": 12,
            "name": "Chemistry"
          },
          {
            "id": 14,
            "name": "Biology"
          },
          {
            "id": 15,
            "name": "Geography"
          }
        ]
      },
      {
        "event_id": 3,
        "subjects": [
          {
            "id": 14,
            "name": "Biology"
          },
          {
            "id": 15,
            "name": "Geography"
          },
          {
            "id": 16,
            "name": "History"
          }
        ]
      }
    ];
    
    console.log(data.reduce((accumulator, currentValue) => {
        accumulator.push(currentValue.subjects.map(x=>x.name));
        return [...new Set(accumulator.flatMap(x=>x))];
    }, []));
    Login or Signup to reply.
  3. const data = [
      {
        "event_id": 1,
        "subjects": [
          {
            "id": 12,
            "name": "Chemistry"
          },
          {
            "id": 13,
            "name": "Physics"
          },
          {
            "id": 14,
            "name": "Psychology"
          },
          {
            "id": 16,
            "name": "History"
          }
        ]
      },
      {
        "event_id": 2,
        "subjects": [
          {
            "id": 11,
            "name": "Maths"
          },
          {
            "id": 12,
            "name": "Chemistry"
          },
          {
            "id": 14,
            "name": "Biology"
          },
          {
            "id": 15,
            "name": "Geography"
          }
        ]
      },
      {
        "event_id": 3,
        "subjects": [
          {
            "id": 14,
            "name": "Biology"
          },
          {
            "id": 15,
            "name": "Geography"
          },
          {
            "id": 16,
            "name": "History"
          }
        ]
      }
    ];
    
    const allSubjects = data.map(x=> x.subjects).reduce((a,x) => [...a, ...x] ,[]);
    const uniqueSubjects = allSubjects.filter((e,i) => allSubjects.findIndex(a => a.id == e.id) == i);
    
    console.log(allSubjects, uniqueSubjects);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search