-3

I am quite a beginner when it comes to programming and currently having a Problem. I have an Array with 4 items (you can see the Array at the Code section below) and all the four items have their specific id (1-4).

The Thing i want to Program is a method which runs individual Code for each Array item. I thought i can solve this Problem by making if Statements where i simply check for the id's (which are individual at every item). But how can i do that??

If someone has a even better idea he can tell me that id for sure.

best regards John.

{ id: 1, name: 'BMW', price: 250, quantity: ''},

{ id: 2, name: 'Google', price: 110, quantity: ''},

{ id: 3, name: 'Apple', price: 1000, quantity: ''},

{ id: 4, name: 'Twitter', price: 50, quantity: ''}
Alon
  • 687
  • 1
  • 6
  • 10
John Kohlmeier
  • 87
  • 1
  • 1
  • 4

4 Answers4

0

You can achieve this with if statement. There are different approaches for this, but the basic approach will remain same. Loop over each element in the array and then check for ids. You can use the traditional for loop or you can use methods like some, filter etc.

Ashish
  • 4,206
  • 16
  • 45
0

You can simply iterate your array using simple for loop and check if id is equal to given id then return the whole object. in case of id is not matched it will return undefined. Consider the following code snippet:

let array = [{ id: 1, name: 'BMW', price: 250, quantity: ''},

{ id: 2, name: 'Google', price: 110, quantity: ''},

{ id: 3, name: 'Apple', price: 1000, quantity: ''},

{ id: 4, name: 'Twitter', price: 50, quantity: ''}];

function getValue(id)
{
  for( var index=0;index<array.length;index++){
     if( array[index].id === id){
        return array[index];
     }
  };
}

console.log(getValue(1));
console.log(getValue(5));
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
0

Loop through and compare the id of each item with the desired text using filter() - note that I can then either return the associuated object (for example - if you want to display the name) - or a text string if its not present.

You could also add logic in there to ensure that the id's are unique - ie if more than one result is found for a give n id - then you need to edit the id of the duplicates.

let items = [
  { id: 1, name: 'BMW', price: 250, quantity: ''},
  { id: 2, name: 'Google', price: 110, quantity: ''},
  { id: 3, name: 'Apple', price: 1000, quantity: ''},
  { id: 4, name: 'Twitter', price: 50, quantity: ''}
]

function findItem(id){
  
  var foundItem = items.filter(function(item){
    return(item.id == id) 
   });
   
   if(foundItem.length > 0) {
    return foundItem[0];
   } else {
    return "Item not found";
   }
}

console.log(findItem('1')); // returns the object of the BMW
console.log(findItem('6')); // returns "Item not found"
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

From what I understand, you want to run separate pieces of code for different items in the array. If the elements of the array are fixed i.e they don't change, then you may write the different pieces of code for each item as function and add it as property to the corresponding item.

See the example below:

let BMWFunction = function(){
   console.log('BMW!');
}

let googleFunction = function(){
   console.log('Google!');
}

let myArray = [
   { id: 1, name: 'BMW', price: 250, quantity: '', code: BMWFunction},
   { id: 2, name: 'Google', price: 110, quantity: '', code: googleFunction }
]

for (let i = 0; i < myArray.length; i++){
   myArray[i].code();
}

then for each item you loop through in the array, you can call the associated code attribute.

aaruja
  • 371
  • 2
  • 12