Sameer Thite
Sameer Thite

Reputation: 2459

javascript iterate through array and perform operation based on conditions

I have following array in my localstorage

[  
   {  
      "customerId":"30",
      "CustomerContactId":63873
   },
   {  
      "customerId":"26506",
      "CustomerContactId":63874
   },
   {  
      "customerId":"26596",
      "CustomerContactId":63875
   },
   {  
      "customerId":"26597",
      "CustomerContactId":63876
   }
]

We have functionality of select customer from dropdown. On select customer, I pass customerId to the function. In function, i call api to generate CustomerContactId for that customerId. i want to iterate through this array and and want to check if customerId is exist in array then do not execute generate CustomerContactId API but if customerId is not exist in whole array, then create CustomerContactId for that customerId. I tried javascript map function. Here this.state.customerContactMap = localstorage array. cust_id = selected customer.

this.state.customerContactMap.map(item => {
   if(item.customerId === cust_id) {
     //Do not generate new CustomerContactId
   } else {
     //Generate CustomerContactId API Call
     //it should only generate CustomerContactId if cust_id is not 
     //exist in array
   }
})

but i am facing problem that it creates CustomerContactId everytime when it goes to else condition. I need help in conditional logic.

Upvotes: 1

Views: 43

Answers (4)

Mohammad Usman
Mohammad Usman

Reputation: 39342

You should use .some() instead of .map().

As per Docs:

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

For example:

if(arr.some(({ customerId }) => customerId === selectedId))
  console.log("Selected Found");
else
  console.log("Selected Not Found");

Demo:

let arr = [{ "customerId": "30", "CustomerContactId": 63873}, {"customerId": "26506","CustomerContactId": 63874}, {"customerId": "26596","CustomerContactId": 63875}, {"customerId": "26597","CustomerContactId": 63876}];

let selectedId = "26596";

if(arr.some(({ customerId }) => customerId === selectedId))
  console.log("Selected Found");
else
  console.log("Selected Not Found");

Upvotes: 0

Atul Sharma
Atul Sharma

Reputation: 10720

You can use Array.filter to check if customer Id exists or not.

var data = [{
"customerId": "30",
"CustomerContactId": 63873
}, {
"customerId": "26506",
"CustomerContactId": 63874
}, {
"customerId": "26596",
"CustomerContactId": 63875
}, {
"customerId": "26597",
"CustomerContactId": 63876
}]

function getCustomer(id){
  return data.filter(function(e){
    if(e.customerId == id){
      return true;
    }
  });
}

var custId = getCustomer("26597");

if(custId.length == 0){
  console.log("Not exists");
  //network call
}else{
  console.log("Exists");
  console.log(custId);
  //normal operations of cust id available
}

Upvotes: 0

user2575725
user2575725

Reputation:

Only having this condition:

 if(item.customerId === cust_id) {

Seems like type of item.customerId and cust_id are not same type.

Try conversion:

 if(item.customerId == cust_id) {

OR

 if(item.customerId === cust_id.toString()) {

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22564

You can use array#some to check if customerId is present in your array.

const customerContactMap = [{ "customerId": "30", "CustomerContactId": 63873 }, { "customerId": "26506", "CustomerContactId": 63874 }, { "customerId": "26596", "CustomerContactId": 63875 }, { "customerId": "26597", "CustomerContactId": 63876 }],
      custId = 30;
if(customerContactMap.some(({customerId}) => customerId == custId)){
  console.log('found');
} else {
  //Execute API
}

Upvotes: 1

Related Questions