0

I created a filter to retrieve the objects from an array. Made use of some of the object values but now I want to use the ids as an array not individually. I know pushing isn't right in this case and since I am still within the filter, how do I achieve this, please?

arrayList = [
  {
     id: 48589,
     height: 5.8,
     active: false
  },
  {
     id: 84697,
     height: 4.6,
     active: true
  },
 {
     id: 887697,
     height: 5.0,
     active: true
   }
 ]

 arrayList.filter(c => {
    if(c.active){
     //`I used the other values here
    } else {
    //Now I need c.id as an array to search my db

       ids = [];
       ids.push(c.id)
    }    
 })
Hopez
  • 141
  • 1
  • 9
  • @Felix Kling This isn't a duplicate question. The answers given do not answer my question – Hopez May 31 '18 at 23:09
  • Then what does *"I want to use the ids as an array not individually."* mean? Please elaborate on your problem. We cannot help you if we don't understand what your are trying to do. Can you explain the higher level goal? – Felix Kling May 31 '18 at 23:14
  • Basically, I want to retrieve data from my db using the [ids] but only if c.active is set to false. I intend to search as follows: customers = await Customers.find({ id: { $in: ids}}) – Hopez May 31 '18 at 23:26
  • So, you want to get all IDs, i.e. have the array `[48589, 84697, 887697]`. Then when you iterate over the objects and `c.active` is `false`, you want to take all these IDs and send them to the server? Or do you only want to get ID of the current object in an array? Please provide a more complete example that contains the expected intermediate values you want to get. – Felix Kling May 31 '18 at 23:39
  • I want to send all the ids as an array [48589, 84697, 887697] to the server. My db contains several customers but using the ids, I want to retrieve only the customers that match the given condition. [ {"name": "test1", "paymentStats": true, "address":"testing address", "paymentDetails":[ { "status": "paid, "date":"2018-05-17T08:14:03.528Z" } "Id":48589 So in this case, I will end up retriving 3 documents from the server – Hopez May 31 '18 at 23:52
  • So then why don't you run `var ids = arrayList.map(c => c.id);` first to get an array of IDs and then iterate over the array again to process each object and use `ids` as necessary? – Felix Kling May 31 '18 at 23:53
  • Tried that already but since it is to be used within the second array, it multiplies the result. – Hopez May 31 '18 at 23:59
  • I don't understand what that means. Again, a more extensive example with actual example values would be helpful. – Felix Kling Jun 01 '18 at 00:00

1 Answers1

2

You need to format your array. Then use map() to create a new array from the existing one:

var arrayList = [
  {
     id: 48589,
     height: 5.8
  },
  {
     id: 84697,
     height: 4.6
  }
]

 var ids = arrayList.map(c => c.id);
 console.log(ids);

Based on the update in the question you can try forEach() and also you have declare ids outside:

var arrayList = [
  {
     id: 48589,
     height: 5.8,
     active: false
  },
  {
     id: 84697,
     height: 4.6,
     active: true
  },
 {
     id: 887697,
     height: 5.0,
     active: true
   }
]
var ids = [];
arrayList.forEach(c => {
  if(c.active){
   //`I used the other values here
  } else {
  //Now I need c.id as an array to search my db
     ids.push(c.id)
  }    
});
console.log(ids);
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • @Mamum I can't use map anymore as I am still within the same loop which is no longer an array – Hopez May 31 '18 at 23:03
  • @Hopez, please check my updated answer....thanks. – Mamun May 31 '18 at 23:20
  • @Mamum Thanks but this only pushes the first item into the array. I want to push all the ids into the array – Hopez May 31 '18 at 23:30
  • @Hopez, how do you expect all `id` with `if(c.active)`? First code snippet will give you all the `id`. – Mamun Jun 01 '18 at 04:34