【问题标题】:Find index of object array查找对象数组的索引
【发布时间】:2019-12-11 03:59:26
【问题描述】:

我被代码卡住了。我实际上想使用 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

}

跟踪:{产品}

产品有 {id,quantity}

dataLayers: { current: { car​​t: { items } } }

商品有{id, brand, name, quantity}

现在我想查找产品的位置/索引,例如:

示例:*

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

{id:'a123',quantity:'1'} 的位置应该是 1 和 {id:'a345',quantity:'2'} 的位置> 应该是 4

我的代码如下所示:

我已经使用 map 从 products 中获取 id,如图所示

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

现在我想在项目中找到这些产品的索引/位置。所以我尝试如下

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

即,

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

但我收到错误

'string[]' 类型的参数不能分配给'(value: ICartItem, index: number, obj: ICartItem[]) => unknown' 类型的参数。 类型 'string[]' 不匹配签名 '(value: ICartItem, index: number, obj: ICartItem[]): unknown'.ts(2345)

有人帮我找到过滤产品的位置/索引?

【问题讨论】:

  • 什么是filter()(为什么和.map()一样)? ivm 是什么?没有 .findindex() 只有 .findIndex() 需要一个函数而不是一个数组。

标签: javascript node.js typescript


【解决方案1】:

map 之后,您可以链接.indexOf() 以获取对象的索引,而a345Index 将是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}`)

【讨论】:

  • 然后只需 +1 到索引
  • 实际上我想找到产品的位置,如果索引为零,那么我需要一个位置为 1。我正在比较产品和项目中的 id。如果项目中存在产品中的 id,那么我需要过滤 id 的位置
  • 我需要传递变量 id 而不是传递 .indexof('a345')。 id 应该与 IProducts 和 ICartItem 进行比较。如果 IProducts 的 id 存在于 ICartItem 中,需要找到对象的 indexof,如描述中的代码 sn-p 所示
【解决方案2】:

您可以链接 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)

如果带有位置的数组也需要返回 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)

【讨论】:

    【解决方案3】:

    你可以试试:

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

    【讨论】:

      猜你喜欢
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多