skip to Main Content

I have below list in javascript

[
 {
    "id" : 111,
    "type": "BASE",
    "someOtherProp": "other",
    "linkedId": 222
 },
 {
    "id" : 222,
    "type": "CHILD",
    "someOtherProp": "other",
    "linkedId": 111
 },
 {
    "id" : 333,
    "type": "SOMEOTHERTYPE",
    "someOtherProp": "other",
    "linkedId": 444
 }
]

I want to check in list if type === 'BASE', get the linkedId of matching type and check in same list if CHILD is present. If CHILD is not present then break the loop
and throw the exception or alert the message.

Note : BASE’s object linkedId is CHILD’s object id.

I am planning to use nested loop but looking a better approach if any for this.

Thank you.

2

Answers


  1. You can avoid nested loops by using a Map to store references to the objects by their id, making it easier and faster to look up related objects. Here’s an approach using reduce and Map:

    const items = [
      {
        id: 111,
        type: "BASE",
        someOtherProp: "other",
        linkedId: 222
      },
      {
        id: 222,
        type: "CHILD",
        someOtherProp: "other",
        linkedId: 111
      },
      {
        id: 333,
        type: "SOMEOTHERTYPE",
        someOtherProp: "other",
        linkedId: 444
      }
    ];
    
    // Create a map of the items based on their `id`
    const itemMap = items.reduce((map, item) => {
      map.set(item.id, item);
      return map;
    }, new Map());
    
    // Find and check BASE and linked CHILD
    items.forEach(item => {
      if (item.type === "BASE") {
        const linkedItem = itemMap.get(item.linkedId);
        
        if (!linkedItem || linkedItem.type !== "CHILD") {
          throw new Error(`CHILD object is not present or linked to BASE with id ${item.id}`);
        }
      }
    });
    
    console.log("All BASE types have a corresponding CHILD");
    

    Efficient way to handle the problem:

    1. Create a Map: Use reduce to create a Map with id as the key and the object as the value. This allows you to look up items by their id in constant time.
    2. Single Iteration: Use forEach to iterate through the list, and for each BASE object, look up its linkedId in the Map.
    3. Check condition: If the linkedId exists and its type is CHILD, the check passes. Otherwise, an error is thrown.

    This avoids nested loops and performs lookups efficiently using the Map.

    Login or Signup to reply.
  2. if you just want to show alert/exception for the type BASE without CHILD

    const items = [
     {
        "id" : 111,
        "type": "BASE",
        "someOtherProp": "other",
        "linkedId": 222
     },
     {
        "id" : 222,
        "type": "CHILD",
        "someOtherProp": "other",
        "linkedId": 111
     },
     {
        "id" : 333,
        "type": "SOMEOTHERTYPE",
        "someOtherProp": "other",
        "linkedId": 444
     }
    ]
    
    const base = items.filter(e=>e.type === 'BASE').map(e=>({id:e.id,linkedId:e.linkedId}))
    const child = items.filter(e=>e.type === 'CHILD').map(e=>e.id)
    
    const baseWithoutChild = base.filter(e=>!child.includes(e.linkedId)).map(e=>e.id)
    if(baseWithoutChild.length){
        alert('BASE Without CHILD:',baseWithoutChild.join(','))
    }
    

    you can do anything with the array baseWithoutChild

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search