0

I am working on Angular 2 with type script and came up with a very peculiar problem. I have a number array named empIds as

empIds = {1,5,6,9,2}

I have (a JSON Array) named empDetails (please note employeeId is string here)as

empDetails [{"employeeId":"1","employeeName":"Jhon"},
            {"employeeId":"2","employeeName":"Ron"},
            {"employeeId":"3","employeeName":"Van"}]

Now i have to find out the only JSON objects who's id match in number array. Here is what i wrote

for(let i=0;i<empDetails.length;i++){
if(empIds.indexOf(empDetails[i].employeeId)==0 ){
//Employee ID is present save that JSON object to some other JSON Array
i should get details for matched id (1 and 2)
}else{
//Did not match
do nothing
}
}

i did put console.log for both if and else. My loop never matches it always goes to else condition? Is there any possible solution?

2
  • 1
    Your empIds = {1,5,6,9,2} is not valid. It should be empIds = [1,5,6,9,2]. Change your condition from if(empIds.indexOf(empDetails[i].employeeId)==0 ){ to if(empIds.indexOf(empDetails[i].employeeId) >= 0 ){ Commented Dec 13, 2017 at 18:54
  • it did not help :( Commented Dec 13, 2017 at 19:35

3 Answers 3

1

The code logic is incorrect. Check index if its greater than -1 Should be as below

 let empIds = [1, 5, 6, 9, 2];
    let empDetails = [{ "employeeId": 1, "employeeName": "Jhon" },
    { "employeeId": 2, "employeeName": "Ron" },
    { "employeeId": 3, "employeeName": "Van" }]
    for (let i = 0; i < empDetails.length; i++) {
      if (empIds.indexOf(empDetails[i].employeeId) > -1) {
        console.log("found");
        console.log(empDetails[i])
      }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Hi All, i made a mistake as the JSON array contains details as string and not numbers.[{ "employeeId": "1", "employeeName": "Jhon" }, { "employeeId": "2", "employeeName": "Ron" }, { "employeeId": "3", "employeeName": "Van" }]
Realized a bit late.. but thanks for all your support
0
const newArray = empDetails.filter(item => empIds.includes(item.employeeId));

I think this is an ok way to get a new array with all the objects from empDetails that have the employeeId in empIds.

.includes() => returns true or false if the empIds contains the employeeId

.filter() => returns all the objects that were true on the include call

empIds = [2];

empDetails = [{"employeeId":1,"employeeName":"Jhon"},
            {"employeeId":2,"employeeName":"Ron"},
            {"employeeId":3,"employeeName":"Van"}]

with my code the new array would be this

[{"employeeId":2,"employeeName":"Ron"}]

because only employeeId = 2 is in empIds table

Comments

0

var filtered = empDetails.filter( (e) => empIds.indexOf(parseInt(e.employeeId)) >= 0 );

If you don't care about support for Internet explorer, you can also use includes() (See answer by Hopless)

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.