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.

7
  • use Array.prototype.filter Commented Nov 16, 2020 at 20:50
  • 1
    Does this answer your question? How to filter object array based on attributes? Commented Nov 16, 2020 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. Commented Nov 16, 2020 at 20:56
  • 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 Commented Nov 16, 2020 at 21:04
  • 1
    Please consider modifying the code here to constitute a minimal reproducible example suitable for dropping into a standalone IDE like The TypeScript Playground 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. Commented Nov 16, 2020 at 21:08

3 Answers 3

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])
Sign up to request clarification or add additional context in comments.

Comments

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]) 
    });

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.