My array looks like this:
array = [object {id: 1, value: "itemname"}, object {id: 2, value: "itemname"}, ...]
all my objects have the same attibutes, but with different values.
Is there an easy way I can use a WHERE statement for that array?
Take the object where object.id = var
or do I just need to loop over the entire array and check every item? My array has over a 100 entries, so I wanted to know if there was a more efficient way
Use the map() method to get an array of values from an array of objects in TypeScript, e.g. const ids = arr. map((obj) => obj.id) . The map method will return a new array containing only the values that were returned from the callback function. Copied!
To remove an object from an array by its value:Call the findIndex() method to get the index of the object in the array. Use the splice() method to remove the element at that index. The splice method changes the contents of the array by removing or replacing existing elements.
To remove an object from a TypeScript array:Use the findIndex() method to get the index of the object. Use the splice() method to remove the object from the array. The splice method will remove the object from the array and will return the removed object.
To find an object in an array by property value: Call the find() method on the array. On each iteration, check if the value meets a condition. The find() method returns the first value in the array that satisfies the condition.
Use Array.find
:
let array = [ { id: 1, value: "itemname" }, { id: 2, value: "itemname" } ]; let item1 = array.find(i => i.id === 1);
Array.find at MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With