简体   繁体   English

查找对象数组的索引

[英]Find index of object array

I'm stuck with the code.我被代码困住了。 I actually want to find the index of elements in dataLayer using typescript/javascript.我实际上想使用 typescript/javascript 在 dataLayer 中找到元素的索引。

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}产品有 {id,quantity}

dataLayers: { current: { cart: { items } } }数据层:{当前:{购物车:{项目}}}

items have {id, brand, name, quantity }物品有{id,品牌,名称,数量}

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 {id: 'a123',quantity: '1'} 的位置应为 1, {id:'a345',quantity:'2'} 的位置应为 4

My code looks like below:我的代码如下所示:

I have used map to get id from products as shown我已经使用地图从产品中获取 id,如图所示

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现在我想在 items.so 中找到这些产品的索引/位置,所以我尝试如下

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

ie, IE,

    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'. 'string[]' 类型的参数不可分配给类型 '(value: ICartItem, index: number, obj: ICartItem[]) => unknown'的参数。 Type 'string[]' provides no match for the signature '(value: ICartItem, index: number, obj: ICartItem[]): unknown'.ts(2345)类型 'string[]' 不匹配签名 '(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 map您可以链接.indexOf()以获取对象的Indexa345 Index将是3而不是4因为索引在数组中zero based

 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}`)

You can chain map and indexOf to get result as an array with positions.您可以链接 map 和 indexOf 以将结果作为带有位置的数组。

 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)

If array with positions needs to return id as well then you can do:如果带有位置的数组也需要返回 id,那么您可以执行以下操作:

 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 => { const id = product.id; return { id, position: items.map(item => item.id).indexOf(id) + 1 } }); console.log(result)

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' } ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM