0

I have this array:

dataArr: DataCode = [dataObj1, dataObj2, dataObj3, dataObj4, ... ]

And array of id:

idArray:string=["3","5","2","8","4"];

Here is the defenition of DataCode:

 export interface DataCode {
  userId: string;
  codeName: string;
  id: string
}

How can I get from array dataArr all objects that has id value as in idArray.

Michael
  • 13,950
  • 57
  • 145
  • 288
  • use [Array.prototype.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Randy Casburn Nov 16 '20 at 20:50
  • 1
    Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – Temoncher Nov 16 '20 at 20:53
  • @RandyCasburn, I am familiar with filter method. In my case in need to filter not by single value but on the collection. – Michael Nov 16 '20 at 20:56
  • @Michael - you mean each id in the collection right? As in iterating `idArray` in order to `filter` `DataCode` on each iteration? – Randy Casburn Nov 16 '20 at 20:59
  • @RandyCasburn, yup. Each item in the collection . – Michael Nov 16 '20 at 21:00
  • 1
    I'll be happy to do it for you, but I need some real data. Otherwise I would probably use `idArray.map(id => dataArr.filter(d=>d.id === id))` - that will give you a nested array of matches for each id in your `idArray` – Randy Casburn Nov 16 '20 at 21:04
  • 1
    Please consider modifying the code here to constitute a [mcve] suitable for dropping into a standalone IDE like [The TypeScript Playground](https://www.typescriptlang.org/play) to demonstrate your issue without extraneous issues, along with enough information for someone to understand what the desired outcome is. As it stands the code here is insufficient for someone to give you an answer they've actually tested against your use case. I'd say you want `idArray.map()` where the callback does either a `filter` or a `find` against `dataArr`, but without concrete and valid data it's hard to tell. – jcalz Nov 16 '20 at 21:08

3 Answers3

4

You can use Array#filter and Array#includes. You will have to filter all the objects which have an id that is present in the ids array. You can see the attached code snippet.

dataArr.filter(item => idArray.includes(item.id))

The above mentioned technique has a time complexity of O(n²). If you want to do it in O(n) time complexity you can try:


dataDictionary = dataArr.reduce((acc, curr) => ({...acc, [curr.id]: curr}))
idArray.map(item => dataDictionary[item])
smtaha512
  • 420
  • 5
  • 11
2

I think it will be helpful to map the id to the objects in a dictionary. This will really help with queries. Although creating a dictionary if you are only using it once might not be the best solution. Then, the filter method is your best friend.

let dataArr = [{'id': 3}, {'id': 5}]
    let idArray =["3","5","2","8","4"];
    let dataDict = {}

    dataArr.forEach(data => {
        dataDict[data.id] = data;
    });

    idArray.forEach(id=> {
        if (id in dataDict)
            console.log(dataDict[id]) 
    });
Talha Azhar
  • 223
  • 2
  • 6
1

You can use filter and indexOf or includes.

Then, using indexOf, checking every value where the index is not -1 implies that exists.
Also using includes if the array has the id, is returned too.

let arr = [{'id': "1"}, {'id': "8"}]
let ids =["1","2","3","4"];

var findIndex = arr.filter(f => ids.indexOf(f.id)!=-1)
var includes = arr.filter(f => ids.includes(f.id))
console.log(findIndex)
console.log(includes)
J.F.
  • 13,927
  • 9
  • 27
  • 65