JavaScript indexOf on array of object instead of array [duplicate] indexof array object javascript,indexof array object typescript,indexof array

This question already has answers here:
Answer:1

Since this is tagged

const x = [{
  "id": "roadshows",
  "name": "Roadshows"
}, {
  "id": "sporting_events",
  "name": "Sporting Events"
}]

console.log( x.findIndex( o => o.id === 'roadshows' ) )
Answer:2

You have to loop through since you have object's inside array.

for(var i = 0; i < x.length; i++) {
    if (x[i].id== 'roadshows') {
        console.log(i);
        break;
    }
}

Or if you just checking that the object exist with that id, filter is handy

if (x.filter(function(e) x.id== 'roadshows').length > 0) {
  // Yay. Do Something
}
Answer:3

manually I'd do something like this:

for(let item of x) {
 if ( item.hasOwnProperty('id') && item['id'] == 'roadshows' ) {   
    //do your stuff here
 }
}
Answer:4

And if you can use es6 and want to return the object in question, then there is always Array.prototype.find()

x.find( item => { return item.id === "roadshows" } )

// returns {id: "roadshows", name: "Roadshows"}
Answer:5

I'm trying to get the progress of a Firebase upload but the function I'm trying to watch the state with is apparently being called on the wrong object type: Uncaught TypeError: uploadTask.on is not a ...

I'm trying to get the progress of a Firebase upload but the function I'm trying to watch the state with is apparently being called on the wrong object type: Uncaught TypeError: uploadTask.on is not a ...

I'm currently trying to receive the width and height of a canvas. The canvas is using a style parameter as you can see in the following code: <canvas id="ctx" style="height: calc(100vh - 142px); ...

I'm currently trying to receive the width and height of a canvas. The canvas is using a style parameter as you can see in the following code: <canvas id="ctx" style="height: calc(100vh - 142px); ...

  1. canvas width height css
  2. canvas width height 100
  3. canvas width height javascript
  4. canvas width height responsive
  5. canvas width height tkinter
  6. canvas width height
  7. canvas width height auto
  8. canvas width height percent
  9. canvas width height style
  10. canvas width height html
  11. canvas width height dynamic
  12. canvas width height percentage
  13. canvas width height parent
  14. canvas width height 0
  15. canvas width height scale
  16. canvas width height change
  17. canvas width height jquery
  18. canvas width height set
  19. canvas width height get
  20. createelement canvas width height

What is the difference between the two? // Chrome console Array(2); // [undefined × 2] Array(2).map(() => 1); // [undefined × 2] [...Array(2)]; // [undefined, ...

What is the difference between the two? // Chrome console Array(2); // [undefined × 2] Array(2).map(() => 1); // [undefined × 2] [...Array(2)]; // [undefined, ...

  1. what difference between bourbon and whiskey
  2. what difference between vegan and vegetarian
  3. what difference between psoriasis and eczema
  4. what difference between flu a and b
  5. what difference between ipad and ipad air
  6. what difference between republic and democracy
  7. what difference between inc and llc
  8. what difference between chromebook and laptop
  9. what difference between md and do
  10. what difference between virus and bacteria
  11. what difference between w2 and w4
  12. what difference between perfume and eau de toilette
  13. what difference between cold and flu
  14. what difference between catholic and christian
  15. what difference between diabetes 1 and 2
  16. what difference between california king and king
  17. what difference between macbook air and pro
  18. what difference between airpod 1 and 2
  19. what difference between medicare and medicaid
  20. what difference between bipolar 1 and 2

I need to build two force graphs using d3 and make them both zoom/pan at the same time. I'm acheving this by forcing the d3.event.scale() and d3.event.translate() properties from within the zoom event ...

I need to build two force graphs using d3 and make them both zoom/pan at the same time. I'm acheving this by forcing the d3.event.scale() and d3.event.translate() properties from within the zoom event ...