Find index of object array

javascript find index of object in array by property
index of object in array javascript
find index of object in array angular 2
angularjs find index of object in array by id
find object in array javascript
find index of element in array java
find index of element in array python
remove element from array javascript

I'm stuck with the code. I actually want to find the index of elements in dataLayer using typescript/javascript.

track: { products },
dataLayers: { current: { cart: { items } } }

products?: IProduct[]

export interface IProduct {
    id: string
    quantity?: string
    name?: string
}
items?: ICartItem[]

export interface ICartItem {
    id: string
    brand: string
    name: string
    quantity: number

}

track: { products }

products have {id,quantity}

dataLayers: { current: { cart: { items } } }

items have {id, brand, name, quantity }

Now I want to find the position/index of products, For Example:

Example: *

products:{
[{id: 'a123',quantity: '1'},{id:'a345', quantity:'2'}]
}

items:{
[{id: 'a123',brand:'pen',name: 'Reynolds', quantity: '1'}, {id: 'a143',brand:'pencil',name: 'Nataraj', quantity: '3'}, {id: 'a122',brand:'pen',name: 'Parker',quantity: '1'},{id:'a345',brand:'Eraser',name: 'Faber-Castell', quantity:'2'}]
}

position of {id: 'a123',quantity: '1'} should be 1 and position of {id:'a345', quantity:'2'} should be 4

My code looks like below:

I have used map to get id from products as shown

const id = products.map(product => { return product.id})

now I want to find index/position of those products in items.so I tried like below

    let position = filter((item: ICartItem) => {
        if(id.includes(item.id)){
            return {id}
        }  
}).findindex(id)

i.e.,

    const id = products.map(product => { return product.id})
    let position = filter((item: ICartItem) => {
        if(id.includes(item.id)){
            return {id}
        }  
}).findindex(id)

but I getting error

Argument of type 'string[]' is not assignable to parameter of type '(value: ICartItem, index: number, obj: ICartItem[]) => unknown'. Type 'string[]' provides no match for the signature '(value: ICartItem, index: number, obj: ICartItem[]): unknown'.ts(2345)

an Some help me in finding position/index of the filtered product?


After map you can chain .indexOf() to get the Index of the object and Index of a345 would be 3 not 4 because indexes are zero based in an array

let items = [{
  id: 'a123',
  brand: 'pen',
  name: 'Reynolds',
  quantity: '1'
}, {
  id: 'a143',
  brand: 'pencil',
  name: 'Nataraj',
  quantity: '3'
}, {
  id: 'a122',
  brand: 'pen',
  name: 'Parker',
  quantity: '1'
}, {
  id: 'a345',
  brand: 'Eraser',
  name: 'Faber-Castell',
  quantity: '2'
}]

let index= items.map(function(e) {
  return e.id;
}).indexOf('a345')

console.log(`Index=${index}`)
console.log(`position= ${index+1}`)

Array.prototype.findIndex(), The findIndex() executes the testFn on every element in the array until it finds the one where testFn returns a truthy value, which is a value that coerces to true . If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values) Otherwise it returns -1; Note: findIndex() does not execute the function for array elements without values. Note: findIndex() does not change the original array.


You can chain map and indexOf to get result as an array with positions.

const products = [
    { id: 'a123', quantity: '1' },
    { id: 'a345', quantity: '2' },
  ];

const items = [
  { id: 'a123', brand: 'pen', name: 'Reynolds', quantity: '1' },
  { id: 'a143', brand: 'pencil', name: 'Nataraj', quantity: '3' },
  { id: 'a122', brand: 'pen', name: 'Parker', quantity: '1' },
  { id: 'a345', brand: 'Eraser', name: 'Faber-Castell', quantity: '2' },
];

const result = products.map(product => items.map(item => item.id).indexOf(product.id) + 1);
console.log(result)

Get the index of the object inside an array, matching a condition , How can I get the index of an obj in an array of objects? const arr = [ {x: "a", y: 1}, { x: "b", y: 2} ] const i Otherwise you have to use findIndex . Here, the if condition is testing if any object in the array is available with id and name equal to the given object. If found, it returns true and moves inside the block. Using findIndex() : findIndex() method is used to find the index of first element in the


You can try:

const ids = products.map( product => product.id);
const positions = items.map( ( item, i ) => {
  if( ids.includes(item.id)){
    return{
      position: i + 1,
      id: item.id
    }
  }
  return null
}).filter(Boolean)

//[ { position: 1, id: 'a123' }, { position: 4, id: 'a345' } ]

JavaScript Array findIndex() Method, Output: 2. The arr.findIndex() method used to return the index of the first element in a given array that satisfies the provided testing function. How To Find The Index of Value in Numpy Array. Python numpy.where() function iterates over a bool array, and for every True, it yields corresponding the element array x, and for every False, it yields corresponding item from array y. So, it returns an array of elements from x where the condition is True and elements from y elsewhere. Syntax


Get index of object in array - JavaScript, findIndex() This method returns the index of first element in array which satisfies the condition. If this method finds an array element for which� Binary search: Binary search can also be used to find the index of the array element in an array. But the binary search can only be used if the array is sorted.Java provides us with an inbuilt function which can be found in the Arrays library of Java which will rreturn the index if the element is present, else it returns -1.


JavaScript Array findIndex() Method, Description. In JavaScript, findIndex() is an Array method that is used to return the index of Duration: 4:52 Posted: Sep 10, 2019 As our array arr is a flat 1D array, so returned tuple will contain only one array of indices and contents of the returned array result[0] are, [ 4 7 11] Get the first index of element with value 15,


indexOf method in an object array in JavaScript, find() method executes a callback function once for each element in the array until it finds a value that returns true. If nothing passes, undefined is� Examples. The example calls the following three overloads of the IndexOf method to find the index of a string in a string array:. IndexOf(Array, Object), to determine the first occurrence of the string "the" in a string array.