skip to Main Content

I have found this previous answer that semi-answers my question but I want to return an array of the indexes in which they appear – in this case ** 18A38** would return the location [1,3].

Entries are only an example – the final version would have potentially hundreds of entries.

const checkArray = ['18A38'] 

const dataOject =
  [ { id:'853K83', isGo:false }
  , { id:'18A38',  isGo:true  }
  , { id:'97T223', isGo:true  }
  , { id:'18A38',  isGo:false }
  , { id:'182B92', isGo:true  }
  ];

const result = checkArray.map(val=>dataOject.findIndex(el=>el.id===val) )


console.log( JSON.stringify(result))

3

Answers


  1. Just loop through and push the indices you need onto an array

    const checkArray = ['18A38'] 
    
    const dataOject =
      [ { id:'853K83', isGo:false }
      , { id:'18A38',  isGo:true  }
      , { id:'97T223', isGo:true  }
      , { id:'18A38',  isGo:false }
      , { id:'182B92', isGo:true  }
      ];
    let foundIndices = []
    for (let i = 0; i < dataOject.length; i++) {
       if (dataOject[i].id === checkArray[0]) {
          foundIndicies.push(i)
       }
    }
    
    return foundIndices
    
    Login or Signup to reply.
  2. checkArray array is of length 1. Of course after using map you’d get only one value back.

    I’d do it that way:

    const checkArray = ['18A38'] 
    
    const dataOject =
      [ { id:'853K83', isGo:false }
      , { id:'18A38',  isGo:true  }
      , { id:'97T223', isGo:true  }
      , { id:'18A38',  isGo:false }
      , { id:'182B92', isGo:true  }
      ];
      
      const result = dataOject.reduce((final, value, index) => {
        if (value.id === checkArray[0]) {
            return [...final, index];
        }
    
        return final;
      }, []);
    
      console.log(result);
    

    If potentially checkArray is of more values, you also could do:

    const checkArray = ['18A38', '853K83'] 
    
    const dataOject =
      [ { id:'853K83', isGo:false }
      , { id:'18A38',  isGo:true  }
      , { id:'97T223', isGo:true  }
      , { id:'18A38',  isGo:false }
      , { id:'182B92', isGo:true  }
      ];
      
      const result = dataOject.reduce((final, value, index) => {
        if (checkArray.includes(value.id)) {
            return [...final, index];
        }
    
        return final;
      }, []);
    
      console.log(result);
    
    Login or Signup to reply.
  3. You can use Array.reduce() to do this

    const checkArray = ['18A38'] 
    
    const dataObject = [
      { id:'853K83', isGo:false },
      { id:'18A38',  isGo:true  },
      { id:'97T223', isGo:true  },
      { id:'18A38',  isGo:false },
      { id:'182B92', isGo:true  },
    ];
    
    const result = dataObject.reduce((acc, val, i) => { 
      if (checkArray.includes(val.id)) {
        acc.push(i)
      }
      return acc;
    }, [])
    
    
    console.log( JSON.stringify(result))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search